Terraform: The importance of the state file.

Coal
3 min readMay 30, 2023

--

In this article, I will attempt to explain a bit about the Terraform state file (tfstate) and why I made the decision to maintain a state file that encompasses the entire application in the framework I am developing, despite it being monolithic and implementing a similar MVC pattern.

The adoption of state has proven to be successful in various scenarios. For example, in front-end development, developers use components like React State or Vuex, leveraging the benefits that state offers. In cloud-focused development, we have Kubernetes, which maintains the state of the cluster in order to facilitate management, and Terraform, which creates cloud environments based on a state file.

These approaches have demonstrated their effectiveness in different contexts, providing a clearer view of the changes made to the application and simplifying administration and resource provisioning in cloud environments.

As I am writing a framework in Go, I opted for using the HCL format to maintain this file. Let’s explore in detail what the Terraform state file is, how it works, and why it is crucial for successful infrastructure management with Terraform.

What is the Terraform State File?

The Terraform state file is a centralized record that maintains detailed information about provisioned resources and their current configuration. It serves as a source of truth for Terraform, enabling the tool to identify differences between the declared configuration (Terraform configuration files) and the current state of provisioned resources. This comparison is crucial for Terraform to determine how to apply the desired changes to the infrastructure.

The state file is typically stored locally or remotely in a backend, such as Amazon S3 or Terraform Cloud, depending on your configuration. It contains essential data, including resource identifiers, attributes, dependencies, and metadata required by Terraform to manage the infrastructure effectively.

By utilizing the state file, Terraform can track the current state of your infrastructure and understand the changes that need to be made to achieve the desired configuration. This enables Terraform to perform operations such as resource creation, modification, and deletion in a predictable and reliable manner.

Importance of the State File

The state file plays a vital role in several functionalities of Terraform:

Planning and Change Management

Terraform utilizes the state file to determine which resources have been created, modified, or removed since the last execution. Based on this information, it generates an execution plan that describes the changes to be applied to the infrastructure. The state file ensures that the changes are made correctly and in accordance with the desired configuration.

Collaboration and Teamwork

The state file allows multiple people to work collaboratively on a Terraform project. It can be shared among the team, enabling each member to have visibility into the current state of the infrastructure. Additionally, version control of the state file can be used to track changes and revert to previous versions if necessary.

Failure Recovery

In the event of a failure during a Terraform execution, the state file stores crucial information about the current state of the infrastructure. This allows Terraform to resume from where it left off, avoiding the need to unnecessarily recreate or modify resources.

Internal Functioning of the State File

The Terraform state file is either a JSON or binary file, depending on the version of Terraform and the configurations used. It contains detailed information about the provisioned resources, such as IDs, addresses, metadata, and specific attributes of each resource.

When Terraform is executed, it loads the existing state file and compares it with the declared configuration in the code. Based on the identified differences, Terraform determines which actions need to be performed to achieve the desired state. During execution, Terraform updates the state file to reflect the changes made to the infrastructure.

It is important to highlight that the state file contains sensitive information, such as passwords or access keys. Therefore, it is crucial to protect and control access to the state file to prevent any leakage of confidential data.

The Terraform state file is a critical component for the proper functioning of this infrastructure-as-code tool. It provides a detailed record of the provisioned resources, allowing Terraform to plan, manage, and apply changes to the infrastructure efficiently. Understanding the internal functioning of the state file is essential to use Terraform effectively and avoid issues related to the management of provisioned infrastructure.

--

--