Interview Prep/Terraform/Refactoring, Migration and Drift

How do you split one state into two without downtime?

AdvancedProductionRefactoring, Migration and Drift
Answer
  1. Back up both states (terraform state pull).
  2. In the new stack, write the config for the resources moving out, and use import blocks with their real IDs (or terraform state mv -state-out).
  3. Run plan in the new stack: it must show no changes.
  4. In the old stack, replace the resource blocks with removed { lifecycle { destroy = false } } and plan: it must show only removals from state.
  5. Apply the new stack, then the old one. No real resource is touched if every plan is clean.
All Terraform questions Roadmap: Terraform in production

More on Refactoring, Migration and Drift