What is Terraform State (tfstate)?
Terraform state (tfstate) is a file that stores your Terraform environment’s current or desired state. The state file is automatically generated by Terraform and contains information about all resources created or modified by Terraform.
This article will provide an overview of Terraform state, discuss how to use Terraform state to manage your infrastructure, and demonstrate how to use Terraform state to restore a previous state of your infrastructure.
What is a State File
To manage your infrastructure, Terraform requires access to its state information. By default, it saves this in a “terraform.tfstate” file. However, storing this information remotely simplifies collaboration.
Terraform starts with the local tfstate file when it creates plans or modifies your infrastructure.
Terraform maps resources in your configuration to objects in a remote system.
When you modify a configuration, Terraform creates a corresponding remote object. It then links that object to a specific resource instance. If you modify this resource later, Terraform might update or remove the linked remote object.
Terraform State helps manage and track your Terraform environment’s configuration. It helps you monitor infrastructure changes, revert to old states, and make new modifications. Think of it as your infrastructure’s version control system.
Understanding Terraform State is crucial when using Terraform for infrastructure management. Now, let’s delve into what Terraform State means and its operations.
Terraform State offers a snapshot of your infrastructure. Stored in a unique format, this file also helps manage your infrastructure’s state.
What is a state file?
A state file is a text file that stores the configuration of a Terraform environment. It is used to manage the state of an environment. For example, you can use state files to store the configuration of an environment, track the changes to that environment, and restore that environment to a previous state.
What do we use it for?
You can use state files to track infrastructure changes, roll back to previous states, and more. You can also use them to perform a variety of complex actions, like:
- Protecting your infrastructure from a Terraform execution failure by rolling back to a previous state and then re-executing Terraform.
- Protecting your infrastructure from a Terraform execution failure by rolling back to a previous state and then running a diff on the current state.