Interview Prep/Terraform/Lost, Corrupted and Drifted State

`Error: Failed to load state` followed by a JSON error like `invalid character '}' looking for beginning of object key string`.

IntermediateTroubleshootingLost, Corrupted and Drifted State
Answer

Likely causes → Someone edited state by hand (trailing comma, bad merge) and uploaded it, or a truncated write from an interrupted upload.

Diagnose →

terraform state pull > broken.json 2>/dev/null || aws s3 cp s3://acme-tfstate/prod/app/terraform.tfstate broken.json
jq . broken.json > /dev/null   # shows the line of the syntax error

Fix → Restore the previous S3 version. If no good version exists, fix the JSON carefully, bump serial by one, and terraform state push.

Prevent → Never hand-edit state. Use terraform state mv/rm, moved, removed and import blocks; they validate and back up.

All Terraform questions Roadmap: Terraform in production

More on Lost, Corrupted and Drifted State