Interview Prep/Terraform/State Backends and Workspaces

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 →

  1. With versioning on, restore the previous version: list with aws s3api list-object-versions, then copy the last good version back.
  2. Without versions, rebuild state with import blocks for each resource, and check plan.
  3. Don't run apply with 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.

All Terraform questions Roadmap: Terraform in production

More on State Backends and Workspaces