Terraform and its State file concept

Linu Bajy
4 min readJan 4, 2023

--

For curious minds , I tried out certain terraform examples to know more about the Terraform State file.

A Terraform state file ( .tfstate extension) is a file that stores the configurations of the infrastructure that has been created. It contains the details of terraform version, resources, outputs and others. Its should NEVER be modified manually.

Let me clarify that the State file might not be exactly the same running infrastructure at some point.

To understand this , lets do a simple example.

I have created an state_example.tf file , that brings up an EC2 instance with an initial tag of “Linu’s First Instance”.

provider "aws" {
region = "us-west-2"
access_key = "mock"
secret_key = "mock"

resource "aws_instance" "myec2" {
ami = "ami-094125af156557ca2"
instance_type = "t2.micro"
tags = {
name = "Linu's First Instance"
}
}

output "ami" {
value = aws_instance.myec2.ami
}

After Terraform apply command, we get a State File (terraform.tfstate) below:

Its all good and expected until now.

I tried an experiment to manually modify the resource from the console.

But looking into the State file, it still has the resource with the old tag. So my concern was, if we do an apply next time, won’t it go back to its old configs, based on the state file?

There are 2 approaches on how this can be resolved to get it updated values on the state file ( terraform.tfstate ).

One fix to do from terraform side is to change the tags in the .tf files manually. After this we do terraform apply and the changes are modified, and its reflected in the State file. But wait, what if there are changes that have been made at many places, that we haven’t kept track of? And also, there must be a way to update without so much manual intervention.

Terraform is smart enough and makes it easier for us. The easiest fix is a not-so-commonly-used command called “terraform refresh”. Terraform APIs will check with the running infrastructure resources and makes updates to the state file. But note that it will not update the Configuration files. So make sure to update it before the next apply command. Now the state file and the configuration files are in sync.

Easy and Simple.

It would be nice to know that terraform apply does the refresh before the terraform apply/plan/destroy command . And that’s why the first method worked. But it doesn’t make any changes to the state file unless the apply command is executed, and its not the ideal way to do it. Apply command should only be used to create new infrastructure or to make significant modifications to the infrastructure.

Terraform refresh does not modify the infrastructure but only makes changes to the state file.

For needs like the above demonstrated example, terraform refresh is the best way to do it.

Terraform State Management

The terraform state command can be used to view/perform actions based on the current state file resource information.

Command : terraform state list

Shows the list of resources with their local names

Command : terraform state show <resource-name>

Detailed information of the resource

Command : terraform state rm <resource-name>

Use Case : If wish to remove a binding to an existing remote object without first destroying it, which will effectively make Terraform “forget” the object while it continues to exist in the remote system.

This would delete the resource information from the state file but this wouldn’t destroy the infrastructure , unless terraform apply command is executed again.

Note : All terraform state commands make changes to the the State File Only. Manual Changes must be done in the current configuration files for the changes to be in sync. This helps to make updates to the state file without destroying and recreating objects for these minor changes.

Repo Link : https://github.com/linubajy/Terraform/

--

--

Linu Bajy
Linu Bajy

Written by Linu Bajy

Enthusiastic Learner . DevOps Professional .

No responses yet