Terraform
●MEDIUM●IaC●INTERMEDIATETerraform State Lock Error
terraform plan or apply fails immediately because the state is locked by a previous, possibly stale, run.
Est. Time
20 minutes
Version
v1.0.0
Updated
7/29/2026
Author
AutoDeploy Team
Tags
Prerequisites
- ✓ Terraform CLI
- ✓ Access to the state backend (S3+DynamoDB, Terraform Cloud, etc.)
Setup Guide
Download guideTerraform State Lock Error
Problem
`terraform plan` or `terraform apply` fails immediately with an error that the state is locked.
Symptoms
- Error acquiring the state lock
- Lock Info block showing ID, Path, Operation, Who, Version, Created
- Started after a previous run was killed (Ctrl+C, CI job canceled, machine crash)
Root Cause
Terraform uses a lock (a DynamoDB row for the S3 backend, or a native lock for Terraform Cloud) so two runs can't write state at once. A run that didn't exit cleanly leaves that lock held even though nothing is actually running anymore.
Solution
1. Read the lock info to identify who/what holds it
terraform plan # note the Lock ID in the error output
2. Confirm no other apply/plan is actually still running (check CI, teammates)
3. If it's confirmed stale, force-unlock using the Lock ID from the error
terraform force-unlock <LOCK_ID>
4. For an S3+DynamoDB backend, you can inspect the lock row directly
aws dynamodb scan --table-name <lock-table-name>
5. Retry the operation
terraform plan
Prevention
- Never kill a running `terraform apply` mid-write — let it finish or fail on its own
- Use a CI pipeline with concurrency limits so two applies against the same state can't start at once
- Keep state locking enabled (the default) rather than disabling it to work around this
- Use short-lived CI runners so a crashed job's lock naturally clears
Last updated on 7/29/2026 · Part of the AutoDeploy DevOps Documentation library