Terraform Life cycle

“terraform knows whether to create a resource or destroy or change a resource from its sate file”

Anything that is referred in the resource block is a desired state

Example

resource "aws_instance" "myec2" {
ami = "ami-55555555555"
instance_type = "t2.small"
}

If we make changes for Ami or instance type manually from console for example you are changing instancetype to t2.large in console

when we do terraform apply terrforrm changes there will be no effect on resource

its always only to t2.small

Example Usage

provider "aws" {
region = "us-west-2"
access_key = "PUT-YOUR-ACCESS-KEY-HERE"
secret_key = "PUT-YOUR-SECRET-KEY-HERE"
}
resource "aws_instance" "myec2" {
ami = "ami-55555555555"
instance_type = "t2.small"
}
```

Steps involed in Terraform lifeCycle

terraform init

  • initializes the required plugin for the provider
  • when we execute terraform init .terraform directory will be created
  • if no version of the provider is specified latest version will be downloaded when we run terraform init
  • its is not mandatory to specify provider version in resource block
  • to specify a particular version we need to use

provider = version

for example:

version = “2.9” downloads provider 2.9 version

version = “>= 2.7” downloads provider >=2.7

version = “<= 2.10”

version = “>=2.10,<=2.15” downloads any provider version with in specified range

terraform plan

> Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure

> it shows the plan of the resources to be created or destroyed or changed

> there will be no change on terraform state file when you execute terraform plan.

> The only case there is change in state file when we do terraform plan is when we mark a resource as taint

Terraform validate

It validates the synatax of terraform code

terraform apply

“`

It creates or destroys or changes the resources based on that are the resource terraform conf file.

Statefile will be effected

Explcit user input yes will be accepted

Terraform destroy

When we do Terraform Destroy it Destroys resources

Sate file will be effected

Explcit user input yes will be accepted

Saving terraform plan

To save a terraform plan terraform plan –out = hello

To apply this plan after few days

terraform apply hello

visit me at www.theterraform.in , Expertise in Aws, terraform, Docker, kubernetes.