How do you split one state into two without downtime?
AdvancedProductionRefactoring, Migration and Drift
Answer
- Back up both states (
terraform state pull). - In the new stack, write the config for the resources moving out, and use
importblocks with their real IDs (orterraform state mv -state-out). - Run plan in the new stack: it must show no changes.
- In the old stack, replace the resource blocks with
removed { lifecycle { destroy = false } }and plan: it must show only removals from state. - Apply the new stack, then the old one. No real resource is touched if every plan is clean.
More on Refactoring, Migration and Drift
What is your workflow for refactoring Terraform safely?IntermediateHow do you rename a resource inside a shared module without breaking consumers?AdvancedHow do you merge two states?IntermediateHow do you import a large existing environment efficiently?IntermediateHow do you upgrade a major version of the AWS provider?AdvancedHow do you upgrade the Terraform CLI across many stacks?Intermediate