Terraform · Infrastructure as code
What is Terraform state and why is it needed?
Short answer
State maps configuration addresses to real resource IDs and stores attributes Terraform needs for planning. Treat it as sensitive operational data, keep it remote, and change it with state commands rather than by hand.
Terraform configuration says what should exist, but it does not contain the cloud IDs of what already exists. State records that mapping, along with dependency metadata and cached attributes. Without it, Terraform cannot tell whether a resource must be created, updated or deleted.
terraform state list
terraform state show aws_s3_bucket.logs
terraform state mv aws_instance.web aws_instance.app
terraform state rm aws_instance.legacyWhat state contains
- Resource addresses and their real provider IDs
- Attribute values, including some that may be sensitive
- Dependency information used to build the graph
- Provider and schema metadata for the last run
Editing state by hand is risky because the format and lineage checks protect against concurrent or stale writes. Use terraform state mv, terraform state rm, moved blocks and import blocks for structural changes. Always back up state before a manual state operation.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What happens if the state file is deleted?
- 2Why is editing state by hand discouraged?
- 3What is the difference between terraform state rm and terraform destroy?
- 4How does state influence the plan graph?
Say it in the interview
State is Terraform's record of which configuration addresses map to which real resources, plus the attributes it needs for planning. Without it, Terraform cannot know whether to create or update. I keep it in a remote backend and change it only through state commands or moved and import blocks. I also treat it as sensitive, because it can contain secrets, and I never assume state loss means the infrastructure is gone.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Terraform mock interview and get scored feedback on depth, clarity and confidence.