Interview Prep/Terraform/State and Backends in Production

State was deleted or corrupted. How do you recover?

AdvancedProductionState and Backends in Production
Answer

With S3 versioning, list object versions of the key and restore the last good one (copy that version over the current object, or delete the delete marker). Then terraform plan should show no diff. Without versioning you are left with terraform import for every resource, which is why versioning, replication and a restore drill are non-negotiable. Run terraform state pull backups nightly in sensitive stacks.

aws s3api list-object-versions --bucket acme-tfstate --prefix prod/network/
aws s3api copy-object --bucket acme-tfstate --key prod/network/terraform.tfstate \
  --copy-source "acme-tfstate/prod/network/terraform.tfstate?versionId=<id>"
All Terraform questions Roadmap: Terraform in production

More on State and Backends in Production