Introduction to the Terraform
Terraform is Hashicorp's infrastructure as a code tool. It lets you define resources and infrastructure in human-readable, declarative configuration files and manages your infrastructure's life cycle.
Terraform can manage mutliplle infrastruture on multiple cloud.
The human-readable configuration language helps you to write infrastructure quickly.
Teraform's state allows you to track resource changes throughout your deployments.
You can commit your configurations to version control safely and collaborate on infrastructure quickly.
Installations:
Terraform Installation on Linux :
Create EC2 instance :
Install package manager for Linux:
HCL:
Hashicorp Configuration language, The syntax of the terraform language is defined in terms of syntax called HCL. Which is also used by configuration language in other applications. and another Hashicorp product. It is not necessary to know all the details of HCL syntax to use Terraform.
To validate the terraform:
Need to initializa the terraform by using terraform init.During that required download will be done.So that resources should not get wasted.
terraform validate: Validate the configuration.
Terraform plan: Wiil dispaly what teraform will do in the backend.
terraform apply: Execution plan will be applied.
Terraform.tfstate: When terraform apply it will generate its logs in the form of "testate"
We can have multiple resources in a single file.
Terraform with Docker.
Terraform needs to be told which provider needs to be used in the automation.Hence we need to give the provider source and version.
terraform { required_providers { docker = { source = "kreuzwerker/docker" version = "~> 3.0.1" } } } ==== Install docker on terraform== terraform block
provider "docker" {} =======It will tell docker provider==Provider block
Creating resources == resource block
resource "docker_image" "nginx" { name = "nginx" keep_locally = false }
resource "docker_container" "nginx" { image = docker_image.nginx.image_id name = "tutorial"
ports { internal = 80 external = 8000 } }
Need to install Docker on the server.
Permission error
Not required to mention IP Since by default IP is exposed