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
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 planafter anystatecommand to verify the result. - Prefer
movedblocks (1.1+) overstate mv— moved blocks are declarative, version controlled and work in CI. state pushwith the-forceflag can overwrite remote state and should be used only as a last resort.state rmdoes not destroy the real resource — it just makes Terraform forget about it. The resource still exists and may drift.
Key Points
state listandstate showare read-only inspection toolsstate mvrenames resources; prefermovedblocks (1.1+) for version-controlled refactorsstate rmmakes 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
- When do you need depends_on in Terraform, and when is it a code smell?
- What is Terraform, and how does it work at a high level?
- How do you configure OIDC authentication between GitHub Actions and AWS for Terraform pipelines?
- How do you use Terraform to deploy the same infrastructure to nonprod and production AWS accounts?
- Terraform apply fails halfway through — some resources were created, others were not. How do you recover?
- Someone manually deletes an EC2 instance that Terraform manages. What happens on the next plan and apply?
- How should nonprod and production Terraform configurations differ, and how do you enforce those differences?
- Walk through terraform init, plan and apply. What does each do?