`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.
More on State Locking and Backend Errors
`terraform plan` fails with `Error acquiring the state lock` and `ConditionalCheckFailedException: The conditional request failed`.BeginnerWith the S3 backend and `use_lockfile = true`, you get `Error acquiring the state lock` with `StatusCode: 412` and `PreconditionFailed`.Intermediate`terraform force-unlock` returns `Failed to unlock state: lock ID "..." does not match existing lock`.BeginnerA teammate ran `terraform apply -lock=false` "because the lock was stuck". Now the plan shows resources you never touched being recreated.Advanced`terraform init` fails with `Error: Backend configuration changed`.Beginner`terraform init -migrate-state` asks `Do you want to copy existing state to the new backend?` and the new backend already has a state. What is the danger?Intermediate