Skip to main content
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.

Terraform Workflow Overview

Imagine you have a project directory named “terraform-local-file” containing the following two files:
The primary Terraform configuration is defined in main.tf as shown below:
The variables used by this configuration are declared in variables.tf:
At this stage, no local file resource has been created yet.

Initializing and Running Terraform Plan

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:
Since the state file does not yet record any resources, Terraform understands that all resources defined in the configuration will be newly created.

Applying the Terraform Configuration

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:
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:
Terraform maintains a state file, which is how it tracks that the resource is already provisioned.

Terraform State File

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:
Inspecting terraform.tfstate reveals a detailed record of the infrastructure, including resource IDs, provider information, and resource attributes:
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.

Updating the Configuration

Consider updating the configuration in variables.tf to modify the content of the file, as shown below:
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:
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:
Now that the configuration file and the state file are in sync, any subsequent runs of terraform apply will report that no changes are necessary.

Conclusion

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.
The image illustrates a comparison between real-world infrastructure and a Terraform state file, using icons for servers, documents, and a dog.
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.

Watch Video