troubleshooting#terraform#state#debugging
Terraform: state lock error (already locked by another process)
Fix the classic 'Error acquiring the state lock' without corrupting your state file.
Problem
Running `terraform apply` fails with `Error acquiring the state lock`. It says the lock is held by another operation, but nobody is running Terraform.
Step-by-step Solution
- 1Read the error message carefully — it shows the lock ID, who acquired it, and how long ago. If the lock was acquired more than a few minutes ago and no one is actively running Terraform, it's stale (a crashed run left it behind).
- 2Ask on the team channel: 'Is anyone running Terraform on <workspace>?' Wait 60 seconds before touching anything.
- 3If confirmed stale, force-unlock: `terraform force-unlock <LOCK_ID>`. Terraform will confirm before removing.
- 4Common causes: a previous run was killed with Ctrl-C too aggressively, a CI job timed out mid-apply, a laptop lost network mid-apply, or DynamoDB (the lock table for the S3 backend) had a hiccup.
- 5Prevent recurrence: enable CI job timeouts *shorter* than the lock timeout; ensure only one CI pipeline touches a given workspace; use `terraform apply -lock-timeout=5m` so runs wait a bit instead of failing instantly.
- 6Never edit the DynamoDB lock table by hand unless you *really* know what you're doing — you can corrupt state by unlocking mid-apply.
See problem and step-by-step solution above.
Keep reading
You may also like
troubleshooting
Kubernetes Pod stuck in CrashLoopBackOff
A methodical checklist to diagnose and fix crash-looping pods in production.
Read
troubleshooting
PostgreSQL: Too Many Connections
The classic Postgres scaling wall — and how PgBouncer solves it.
Read
troubleshooting
Nginx 502 Bad Gateway after Deploy
Nine times out of ten it's one of these five issues — here's how to isolate them.
Read
Discussion (0)
No comments yet. Be the first to weigh in.