State Locking
State locking stops two Terraform runs from writing the same state at once. This lesson explains how locking works, which backends support it and how to deal with locks that are held or left behind.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why and when Terraform locks state.
- Identify backends that support locking, including S3 native locking.
- Describe how to handle lock timeouts and stale locks.
Why and when Terraform locks
If two people apply the same configuration at the same moment, both read the same state, both make changes and both write state back; the second write discards the first person's changes, and Terraform loses track of real resources. State locking prevents this. For every operation that could write state, such as plan, apply, destroy, import, refresh-only runs and the terraform state commands that modify state, Terraform acquires a lock first and releases it at the end.
Error: Error acquiring the state lock
Lock Info:
ID: 8c1d2f0e-5b7a-4c3e-9f21-0d6a4e2b9c11
Path: acme-tfstate/network/terraform.tfstate
Operation: OperationTypeApply
Who: jo@build-agent-7
Created: 2026-10-25 09:14:02 UTCLocking is automatic; there is nothing to enable on the Terraform side if the backend supports it. If the lock is held, the command fails and shows who holds it.
Backend support
Not all backends can lock. The local backend locks with file system locks on one machine. Remote backends that support locking include Amazon S3, azurerm (blob leases), gcs, consul, pg, kubernetes and HCP Terraform, which also queues runs per workspace. For S3, Terraform 1.10 introduced native S3 locking with use_lockfile = true, which writes a .tflock object next to the state using S3 conditional writes, and Terraform 1.11 made it generally available. The older method, a DynamoDB table named in dynamodb_table, is deprecated and will be removed in a future release.
Locking support
Waiting for and removing locks
-lock-timeout=DURATION, such as -lock-timeout=5m, makes Terraform retry until the lock is free or the time is up, which suits pipelines where runs may briefly overlap. -lock=false disables locking for one command; it is dangerous and should be avoided.
If a run crashes or loses its connection, the lock can remain after the process has gone. terraform force-unlock LOCK_ID removes it after a confirmation prompt; -force skips the prompt. The command only releases the lock; it changes neither infrastructure nor state. Force-unlock only after confirming that the run that held the lock has really stopped, otherwise two runs may write state at once.
-
Read the lock information
Note the ID, who holds the lock and when it was created.
-
Confirm the holder has stopped
Check the pipeline or ask the person named in Who.
-
Run terraform force-unlock LOCK_ID
Release the stale lock.
-
Re-run the operation
Plan again, because state may have been partly updated by the failed run.
The following diagram shows the safe recovery from a stale lock.

Dealing with locks
A stuck pipeline
A CI job running terraform apply was cancelled half-way. Every later job fails with "Error acquiring the state lock", naming the cancelled job's runner. What should the team do?
The decisive fact is that the lock outlived its process.
After confirming that the cancelled job's runner is no longer running Terraform, an engineer runs terraform force-unlock with the lock ID from the error. The next job should run a plan, because the cancelled apply may have created some resources and recorded them before stopping. Adding -lock-timeout to jobs would not help here, because the lock will never be released by its holder.
Which S3 backend setting enables native state locking?
Show answer
use_lockfile = true. Locking with a DynamoDB table is deprecated.
Exam Tips
Remember for the exam
- Locking is automatic for operations that write state, if the backend supports it.
- S3 native locking: use_lockfile = true; DynamoDB locking is deprecated.
- -lock-timeout waits; -lock=false disables locking (avoid).
- force-unlock LOCK_ID releases a stale lock only.