Move from local state to S3 for an existing project without losing anything.
IntermediateScenarioState Backends and Workspaces
Answer
Situation read → Existing resources, existing local state.
What I'd do →
- Back up
terraform.tfstate. - Add the
backend "s3"block. - Run
terraform init -migrate-stateand answeryesto copy. - Run
terraform plan; it should be clean. - Delete the local state file after verifying the remote object.
Why → -migrate-state copies state with its lineage and serial, so nothing is re-imported.
Interview tip → Check the plan is empty before telling anyone it worked.
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