Terraform interview question · Most asked

What are the main terraform state subcommands, and when do you use them?

Definition Asked in DevOps Engineer · Cloud Engineer · Site Reliability Engineer (SRE) interviews

Members can save this page, take notes and track progress. See membership · Sign in

Summary

terraform state list shows all resources in state, show prints one resource's attributes, mv renames or moves a resource without destroying it, rm removes a resource from state without deleting the real infrastructure, and pull/push read and write the raw state file. These are operational commands for fixing state, not part of normal workflow.

Longer Answer

Command What it does When you use it
state list Lists every resource address in the current state Auditing what Terraform manages; scripting bulk operations
state show <addr> Prints all attributes of one resource Checking the actual values Terraform recorded (IDs, ARNs, tags)
state mv <src> <dst> Renames a resource in state Refactoring: you renamed a resource or moved it into a module. Without mv, Terraform would destroy the old name and create the new one. (Terraform 1.1+ moved blocks are preferred for this.)
state rm <addr> Removes a resource from state without deleting it Handing a resource to another Terraform configuration or to manual management
state pull Prints the raw JSON state to stdout Debugging, scripting, or piping into jq for analysis
state push Writes a JSON state file to the backend Disaster recovery: restoring a manually edited or recovered state file
state replace-provider Changes the provider for resources in state Migrating between provider forks (e.g. hashicorp/aws to a custom fork)

Safety rules:

  • Always run terraform plan after any state command to verify the result.
  • Prefer moved blocks (1.1+) over state mv — moved blocks are declarative, version controlled and work in CI.
  • state push with the -force flag can overwrite remote state and should be used only as a last resort.
  • state rm does not destroy the real resource — it just makes Terraform forget about it. The resource still exists and may drift.

Key Points

  • state list and state show are read-only inspection tools
  • state mv renames resources; prefer moved blocks (1.1+) for version-controlled refactors
  • state rm makes Terraform forget a resource without destroying it
  • Always run a full plan after any state manipulation to verify consistency

Explanation

What interviewers look for

Look for comfort with the operational side of Terraform. Weak answers confuse state rm with terraform destroy. Strong answers recommend moved blocks over state mv for routine refactors and emphasise the plan-after-state-change discipline.

More Terraform questions

← All Terraform questions

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake