Wednesday, August 19, 2020

Terraform State file

Previous

This is the third blog post of terraform series. In this blog post, we are going to discuss terraform state file.

We are going to discuss below topics.

Terraform State file
 Current State
 Desired State
Terraform State command
 terraform refresh
 terraform show
 terraform state list
Terraform provider version

Terraform State file

To understand the concept of terraform state file, we need to think about below scenario.

when we perform terraform plan/apply/destroy, how terraform will identify, what action needs to be taken against each resource, which is mentioned in terraform configuration file.

The Answer is “terraform is using some kind of database to store the current state of resources”. Database/storage, where terraform stores sate of all resources, which is created by terraform configuration file is called terraform “state file”. The extension of terraform state file is “. tfstate”.

Terraform state file is a JSON format file. when resources are being add/update/delete using terraform configuration file via apply command, terraform updates the associated state file automatically.

With the help of terraform state file, terraform is able to map the real world resources to terraform configuration file.

Every resource is unique identify in a state file by combination of “resource Name” and “Logical Name”.

Unique name of below resource in state file is  aws_instance.LogicalName .


Terraform provides different type of backed configuration options to store the state file, which we will discuss in later post.

Desired State

This is resource state, which is mentioned in terraform configuration file under resource section.

e.g  In above example, we have mentioned “Instance type” is “t2.micro”, so t2.micro is desired state property for our EC2 instance.

Current State file

This is an actual resource state of our current running infrastructure.  

Terraform updates the current state of your resource in “terraform state” file, while we create resources using terraform, so state file represents the current state of your infrastructure, if resources state is not being changed manual outside the terraform.

e.g  suppose we have created one EC2 instance with “t2.micro” instance type on AWS and someone changed the instance type to “t2.nano“from AWS console. In this case current state of your resource is “t2.nano” and desired state is “t2.micro”

Terraform State command

Command -1

 # terraform refresh

This command is used to update the terraform state file with current state of resource.

If someone changed the state of resource manually from backend. Now resource current state and state in “state file” is different. terraform provides “terraform refresh” command to update the state file as per the current state of resource.

When we perform “terraform plan” or “terraform apply” , terraform first executes “terraform refresh” command to update the state file internally .

Terraform refresh

Command -2

# terraform show

This command is used to see the terraform state  file contents on terminal window in readable format.

Command -3

# terraform state list

The command will list all resources in the state file. 

  Previous


Cheers!
Sandeep 
https://www.linkedin.com/in/sandeep-sharma-40a40b22/

No comments:

Post a Comment

Terraform State file

Previous This is the third blog post of terraform series. In this blog post, we are going to discuss terraform state file. We are going to...