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.
terraform state list # tracked resourcesterraform state show aws_s3_bucket.order_imagesterraform state mv OLD_ADDRESS NEW_ADDRESS # rename without destroyingterraform state rm ADDRESS # stop tracking, do not destroySecurityState stores attributes in plaintext, including passwords and keys. Use an encrypted remote backend and restrict read access.
Common MistakeCommitting
terraform.tfstateto 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.