Interview Prep/Terraform/State Locking and Backend Errors

`Error refreshing state: state data in S3 does not have the expected content. The checksum calculated for the state stored in S3 does not match the checksum stored in DynamoDB.`

AdvancedTroubleshootingState Locking and Backend Errors
Answer

Likely causes → With the legacy DynamoDB lock table, Terraform also stores an MD5 digest (<bucket>/<key>-md5 item). Someone uploaded or restored state directly in S3, or a previous write failed halfway.

Diagnose → Check S3 object versions and the -md5 item in the table:

aws dynamodb get-item --table-name terraform-locks \
  --key '{"LockID":{"S":"acme-tfstate/prod/app/terraform.tfstate-md5"}}'

Fix → Wait a minute and retry (it can be a delay). If it persists and the S3 object is the version you want, update the Digest to the "Calculated checksum" shown in the error, or delete the -md5 item.

Prevent → Restore state through terraform state push, not aws s3 cp. Moving to use_lockfile = true (Terraform 1.10+) removes the DynamoDB digest entirely.

All Terraform questions Roadmap: Terraform in production

More on State Locking and Backend Errors