state lock
What is state lock?
state lock: A mutex that stops two people running apply at once and corrupting state. The S3 backend uses DynamoDB (or native locking) for this. A state lock stops two Terraform runs from changing the same state at the same time. When someone runs plan or apply, Terraform takes the lock; anyone else has to wait until it is released.
Frequently asked questions about state lock
How is state lock used in practice?
Without locking, two simultaneous applies can both read the old state, make different changes and overwrite each other's records. The state then no longer matches reality, which is painful to repair.
What should you keep in mind with state lock?
Most remote backends support locking; the S3 backend uses DynamoDB or, in newer versions, native S3 locking. If a run crashes and leaves the lock behind, terraform force-unlock with the lock ID releases it. Only do that when you are sure no other run is still active, because unlocking during a real apply is exactly what locking is meant to prevent.
What does an example of state lock look like?
terraform force-unlock At a glance
| Category | Terraform |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |