A giant state with 1,500 resources must be split into network and app states. How do you do it safely?
AdvancedScenarioState Backends and Workspaces
Answer
Situation read → You must move resources between states without destroying anything.
What I'd do →
- Back up the state (
terraform state pull > backup.tfstate). - Write the new root module, with the same resource code.
- Use
terraform state mv -state-out=new.tfstate ...for each resource, orimportblocks in the new stack andremovedblocks in the old one. - Push the new state to the new backend and run
terraform planin both: both must be clean. - Do it in a maintenance window with locks held.
Why → Each step is reversible via the backup, and clean plans prove the split is correct.
Interview tip → Prefer import and removed blocks for reviewability; state mv is imperative.
More on State Backends and Workspaces
Two engineers keep overwriting each other's changes because state is local. What's the fix?BeginnerSet up a production-grade S3 backend. What do you configure for locking?IntermediateHow do you harden the S3 bucket that holds Terraform state?IntermediateWho creates the state bucket itself, given Terraform needs it to exist before `init`?IntermediateA laptop apply and a CI apply start at the same time. What stops state corruption?IntermediateWhen are Terraform workspaces appropriate?Beginner