Terraform State
Terraform state is a JSON file (terraform.tfstate) where Terraform and OpenTofu store the mapping between the resources in your code and the real objects in the cloud, plus their attributes. On every plan, the tool refreshes the state, compares it with the code and calculates the changes.
Why it matters #
- Without the state, Terraform could not know that
aws_instance.webis the instancei-0abc123. - It contains sensitive data in plain text (passwords, keys), so it must be protected: see secrets management and state encryption.
- It must be shared and locked in a team, which is the job of a backend.
- Never edit it by hand. Use
terraform statecommands or import, moved and removed blocks.
Related #
Backends tutorial, workspaces, drift and the cheat sheet.