The Terraform State File

Understand how Terraform state files work.

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.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy