terraform apply fails with Error acquiring the state lock — diagnose and safely resolve it
Your CI terraform apply fails immediately with the error below. A previous pipeline run was cancelled mid-apply an hour ago. Explain what a state lock is, why this happened, and the SAFE way to resolve it — including what you must verify before doing anything forceful.
Error: Error acquiring the state lock
Lock Info:
ID: 9db590f1-4c2a
Operation: OperationTypeApply
Who: ci-runner@build-42
Created: 1 hour ago
Info: s3 backend / dynamodb lock table
Terraform acquires a state lock to protect your state from writes
by multiple users at the same time.
Diagnose the cause and give the safe resolution.
The backend lock protects state from concurrent writes. The cancelled run died before releasing its lease, so the lock is stale — nothing is applying. Verify no apply is live from the Who/Created fields, then terraform force-unlock with the shown ID. Never force it blindly.
- ✗Force-unlocking without first checking whether an apply is live
- ✗Deleting the state file or lock table to clear the error
- ✗Assuming the lock expires on its own after a timeout
- →Why is a blind
force-unlockdangerous during a real concurrent apply? - →How does a DynamoDB lock table serialize applies against an S3 backend?
The backend lock serializes writes to state so two applies cannot interleave them. The cancelled run died without releasing its lease, so it is stale — an orphaned lock, not an active apply.
Safe procedure
# 1. Confirm a real apply is NOT running: the Who/Created fields + the CI pipeline status.
# 2. Only then release the stale lock by its ID:
terraform force-unlock 9db590f1-4c2a
# 3. Retry the apply.
Do not delete the state file or wipe the lock table — that loses data or breaks the backend. A force-unlock during a live apply will corrupt state.