This article explores Terraform state management, detailing how it tracks infrastructure changes and the significance of the state file in managing resources.
Use this file to discover all available pages before exploring further.
In this article, we expand on our introduction to Terraform by exploring how state management works under the hood. Building on our foundational knowledge of writing simple Terraform configurations with HCL, declaring variables, using reference expressions, and linking resources, we now dive into Terraform state to understand how Terraform tracks real-world infrastructure changes.
Before provisioning any resources, initialize Terraform by executing the terraform init command. This command downloads the necessary plugins. Next, generate an execution plan with the terraform plan command. Notice that Terraform refreshes the state in memory (even if no state exists yet) and computes an execution plan that indicates which resources will be created.The output of the plan command is similar to the following:
[terraform-local-file]$ terraform planRefreshing Terraform state in-memory prior to plan...The refreshed state will be used to calculate this plan, persisted to local or remote state storage.An execution plan has been generated and is shown below.Resource actions are indicated with the following symbols: + createTerraform will perform the following actions: # local_file.pet will be created + resource "local_file" "pet" { + content = "I love pets!" + directory_permission = "0777" + file_permission = "0777" + filename = "/root/pets.txt" + id = (known after apply) }Plan: 1 to add, 0 to change, 0 to destroy.Note: You didn’t specify an -out parameter to save this plan, so it will not be possible to replay this exact plan thereafter.
Since the state file does not yet record any resources, Terraform understands that all resources defined in the configuration will be newly created.
To apply the configuration, run the terraform apply command. This command will reinitialize the in-memory state, confirm that no state file exists yet, and then proceed to create the local file resource:
$ terraform applyAn execution plan has been generated and is shown below.Resource actions are indicated with the following symbols: + createTerraform will perform the following actions: # local_file.pet will be created + resource "local_file" "pet" { + content = "I love pets!" + directory_permission = "0777" + file_permission = "0777" + filename = "/root/pets.txt" + id = (known after apply) }Plan: 1 to add, 0 to change, 0 to destroy.Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.Enter a value: yeslocal_file.pet: Creating...local_file.pet: Creation complete after 0s[id=7e4db4fbfddb108bdd046926202bae3e9bd1e168]
Upon confirmation, Terraform creates the resource, generates a unique ID for it, and creates the file /root/pets.txt with the content “I love pets!” If you run terraform apply again, Terraform refreshes the state, detects that the resource already exists, and confirms that no further actions are needed:
After the initial successful terraform apply, an additional file named terraform.tfstate is created in the project directory. This file is a JSON data structure mapping your real-world infrastructure to the resource definitions from your configuration files. The directory now appears as follows:
This state file is the single source of truth for Terraform. It is leveraged during subsequent commands such as terraform plan and terraform apply to determine if any changes to the infrastructure are required.
After this change, running terraform apply causes Terraform to refresh the state and detect a difference between the new configuration and the existing state. Consequently, Terraform decides the resource must be replaced:
$ terraform applylocal_file.pet: Refreshing state... [id=7e4db4fbfdb108ddb04692602bae3e9bd1e1b68]Terraform will perform the following actions:# local_file.pet must be replaced-/+ resource "local_file" "pet" { directory_permission = "0777" file_permission = "0777" filename = "/root/pets.txt" ~ id = "7e4db4fbfdb108ddb04692602bae3e9bd1e1b68" -> "7e4db4fbfdb108ddb04692602bae3e9bc4d1c14" # "We love pets!" # forces replacement due to content change }
After applying these changes, Terraform deletes the old resource and creates a new one with a different unique ID. The updated terraform.tfstate now reflects the new state:
In this lesson, we explored how Terraform leverages a state file—initially created during the first successful apply—to track and manage real-world infrastructure. This state file serves as the authoritative record for your resources and is essential for Terraform to efficiently plan and apply configuration changes.
Managing your Terraform state is crucial for ensuring consistent and predictable infrastructure behavior. For more information, consider reading the Terraform State documentation.
In upcoming lectures, we will further explore the significance of state management in Terraform and discuss best practices for handling it effectively. For additional Terraform guides, visit the Terraform Documentation.