Terraform · Infrastructure as code

What is Terraform state and why is it needed?

MediumstatemappingoperationsUpdated Oct 2026
Quiz this track

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.

sh
terraform state list
terraform state show aws_s3_bucket.logs
terraform state mv aws_instance.web aws_instance.app
terraform state rm aws_instance.legacy
State commands change Terraform's bookkeeping. They do not create or delete the real resource.

What 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.

State is not a backup of your infrastructure and not a replacement for configuration. If state is lost, the resources still exist; you must re-import or rebuild the mapping.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What happens if the state file is deleted?
  2. 2Why is editing state by hand discouraged?
  3. 3What is the difference between terraform state rm and terraform destroy?
  4. 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.