2017-02-14 23:46:20 +01:00
|
|
|
---
|
2020-08-15 03:51:06 +02:00
|
|
|
layout: "language"
|
2017-02-14 23:46:20 +01:00
|
|
|
page_title: "State: Locking"
|
|
|
|
sidebar_current: "docs-state-locking"
|
|
|
|
description: |-
|
|
|
|
Terraform stores state which caches the known state of the world the last time Terraform ran.
|
|
|
|
---
|
|
|
|
|
|
|
|
# State Locking
|
|
|
|
|
2021-01-16 00:41:43 +01:00
|
|
|
If supported by your [backend](/docs/language/settings/backends/index.html), Terraform will lock your
|
2017-02-15 21:58:52 +01:00
|
|
|
state for all operations that could write state. This prevents
|
|
|
|
others from acquiring the lock and potentially corrupting your state.
|
|
|
|
|
|
|
|
State locking happens automatically on all operations that could write
|
|
|
|
state. You won't see any message that it is happening. If state locking fails,
|
|
|
|
Terraform will not continue. You can disable state locking for most commands
|
|
|
|
with the `-lock` flag but it is not recommended.
|
|
|
|
|
|
|
|
If acquiring the lock is taking longer than expected, Terraform will output
|
|
|
|
a status message. If Terraform doesn't output a message, state locking is
|
2017-04-26 16:10:04 +02:00
|
|
|
still occurring if your backend supports it.
|
2017-02-15 21:58:52 +01:00
|
|
|
|
2021-01-16 00:41:43 +01:00
|
|
|
Not all backends support locking. The
|
|
|
|
[documentation for each backend](/docs/language/settings/backends/index.html)
|
|
|
|
includes details on whether it supports locking or not.
|
2017-02-15 21:58:52 +01:00
|
|
|
|
|
|
|
## Force Unlock
|
|
|
|
|
2021-01-19 22:43:01 +01:00
|
|
|
Terraform has a [force-unlock command](/docs/cli/commands/force-unlock.html)
|
2017-02-15 21:58:52 +01:00
|
|
|
to manually unlock the state if unlocking failed.
|
|
|
|
|
|
|
|
**Be very careful with this command.** If you unlock the state when someone
|
|
|
|
else is holding the lock it could cause multiple writers. Force unlock should
|
|
|
|
only be used to unlock your own lock in the situation where automatic
|
|
|
|
unlocking failed.
|
|
|
|
|
|
|
|
To protect you, the `force-unlock` command requires a unique lock ID. Terraform
|
|
|
|
will output this lock ID if unlocking fails. This lock ID acts as a
|
|
|
|
[nonce](https://en.wikipedia.org/wiki/Cryptographic_nonce), ensuring
|
|
|
|
that locks and unlocks target the correct lock.
|