Interview Prep/Terraform/State Locking and Backend Errors

A teammate ran `terraform apply -lock=false` "because the lock was stuck". Now the plan shows resources you never touched being recreated.

AdvancedTroubleshootingState Locking and Backend Errors
Answer

Likely causes → Two applies wrote state concurrently. The last writer won, so the state no longer records objects the other run created. Terraform now plans to create (duplicate) or destroy things based on an incomplete state.

Diagnose →

aws s3api list-object-versions --bucket acme-tfstate --prefix prod/app/terraform.tfstate \
  --query 'Versions[].{v:VersionId,t:LastModified,size:Size}'
terraform state list | sort > now.txt

Download the previous versions and diff terraform state list output between them.

Fix → Freeze applies. Either restore the most complete state version and terraform import what the other run created, or keep the current one and import the missing objects. Re-plan until it shows only intended changes.

Prevent → Remove the habit: block -lock=false in CI wrappers, and teach force-unlock with verification instead.

All Terraform questions Roadmap: Terraform in production

More on State Locking and Backend Errors