Search⌘ K
AI Features

The Terraform State File

Explore how Terraform uses state files to track managed resources and what happens when state files are removed. Learn to apply changes with the -auto-approve flag and understand its risks to maintain infrastructure consistency.

A .tfstate file was created when we ran terraform init in the previous lesson. Here, we’ll examine that file and see what happens if we lose it. The more we use Terraform, the more important it is to understand Terraform state management, especially if we work in teams.

A look at the state file

To get a state file, we create a new directory named ltthw_state and a hello_state.tf file within it. Then we execute terraform init and terraform apply. This time we add a new flag: -auto-approve.

The -auto-approve gets rid of the need to say yes each time we run apply. This is very handy if we’re sure we’re going to say yes before we hit return.

It’s less handy if we run apply through muscle memory and ignore that it might generate a plan that involves destroying key parts of our infrastructure. This can and will happen if we use -auto-approve all the time, so we have to be careful when using it.

Note: You have to run all the commands in the following code snippet to observe how Terraform will work.

resource "local_file" "hello_state_file" {
   content = "Hello terraform state!"
   filename =  "${path.module}/hello_state.txt"
}
The ltthw_state module

We now see a state file named terraform.tfstate if we look at the files in this directory using the following command:

ls 

Then we cat the file to take a look at its contents using the following:

cat terraform.tfstate

We don’t need to be concerned with every line in the above file, but a quick read-through should help us understand that it stores the state of the resources that our Terraform module is interested in.

Pay close attention to the resources section because that is the most relevant to this module. Now we are going to remove the state file to see what difference that makes using the following command:

rm terraform.tfstate

We can confirm that the text file we are managing with this Terraform module is still there by using the following:

cat hello_state.txt

Now, we reapply the module using the following:

terraform apply -auto-approve

What did that do? It planned to add the file, re-created the state file, and also rewrote the file under its control even though the contents were unchanged.

We can check this by waiting a minute and doing it again, followed by a ls -l to check the hello_state.txt file’s timestamp.

Cleanup

To clean up, we’ll simply type the following commands:

Shell
terraform destroy -auto-approve
cd ..
rm -rf ltthw_state

What we learned

We covered the following points:

  • How to -auto-approve the terraform apply command (and why this can be dangerous).
  • What a Terraform state file looks like.
  • What happens if we remove the state file and re-run terraform apply.

Quiz

Test your knowledge of Terraform State File

1.

What is the name of the Terraform state file?

A.

terraform.tfstate

B.

tf.state

C.

state.tf

D.

terraform.state


1 / 3