The state file in S3 is deleted by mistake. How do you recover?
AdvancedScenarioState Backends and Workspaces
Answer
Situation read → Infrastructure is fine, Terraform has amnesia.
What I'd do →
- With versioning on, restore the previous version: list with
aws s3api list-object-versions, then copy the last good version back. - Without versions, rebuild state with
importblocks for each resource, and check plan. - Don't run
applywith an empty state; it would try to create duplicates.
Why → Versioning turns a disaster into a one-minute fix; without it, recovery is slow and error-prone.
Interview tip → Add MFA delete or object lock for prod state buckets.
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