Skip to main content

Terraform State File

The Terraform state file, terraform.tfstate, is a JSON file where Terraform records the infrastructure it manages. It maps each resource in your configuration to its real cloud ID and stores the attributes Terraform last saw.

What Is the State File?

State is Terraform's memory. Cloud APIs cannot say which real objects belong to which blocks in your code, so state stores that mapping. Do not edit it by hand. Use the CLI.

Bash
terraform state list # tracked resources
terraform state show aws_s3_bucket.order_images
terraform state mv OLD_ADDRESS NEW_ADDRESS # rename without destroying
terraform state rm ADDRESS # stop tracking, do not destroy
Security

State stores attributes in plaintext, including passwords and keys. Use an encrypted remote backend and restrict read access.

Common Mistake

Committing terraform.tfstate to Git. It leaks secrets and causes conflicts. Use a remote backend with locking.

Frequently Asked Questions

Why can Terraform not just read the cloud instead of using state?

Cloud APIs do not know which resources belong to which blocks in your code. State provides that mapping plus metadata that cannot be inferred.

How do I rename a resource without destroying it?

Add a moved block from the old address to the new one, or run terraform state mv, then confirm the plan shows no destroy.

Found a mistake, an outdated step or a broken link?