State file overview
Categories:
less than a minute
Every time Terraform runs, it records information about what infrastructure has been worked with in a state file
. By default, assuming you haven’t configured remote backend
state this file is saved to the Terraform working directory as two files called terraform.state
and terraform.state.backup
. The state file is a JSON file and can be read by you if required in any text editor.
Warning
Never edit the state file, as doing so could corrupt or invalidate the data. The Terraform API should only ever update the state file.When you run terraform plan
, Terraform reads the current state of the infrastructure resources from the state file
and compares it to the desired state specified in the configuration files
. It then calculates the difference, or plan
, between the two states and shows you what actions Terraform would take to bring the actual resources into alignment with the desired state. This allows you to preview the changes that would be made before applying them, helping to prevent unintended or destructive changes to your infrastructure.