State Lock
A Terraform state lock prevents two operations from modifying the same state at once. While one run holds the lock, other runs wait or fail with an error showing who holds it.
What Is a State Lock?
The backend holds a lock record while Terraform changes state. S3 uses a lock file with use_lockfile = true (Terraform 1.11 and later) or a DynamoDB table with a LockID key on older setups. Terraform Cloud, GCS, and Azure Blob lock natively.
terraform apply -lock-timeout=5m # wait up to 5 minutes for the lockterraform force-unlock LOCK_ID # only after confirming nothing is runningSecurityNever force-unlock while an apply might still be running. Two simultaneous writers can corrupt state.
Remember
-lock=falsedisables locking. Avoid it in shared environments.
Frequently Asked Questions
Why do I need locking if I am the only user?
A pipeline or second terminal can run at the same time as you. Locking protects against any concurrent operation, not only other people.
When is force-unlock safe?
Only after you confirm the original operation has ended, for example the CI job was killed. Check the lock holder and timestamp in the error first.