Terraform Associate 004 ยท State and backends
State locking
Why locking exists, which backends support it, automatic locking on write operations, -lock-timeout, force-unlock (6b).
Key points
- If the backend supports it, Terraform locks state automatically for every operation that could write it, so two runs cannot write at once. -lock-timeout makes a run wait for the lock (default 0s, fail at once). terraform force-unlock LOCK_ID releases a stuck lock; -lock=false skips locking and is discouraged.
Source: https://developer.hashicorp.com/terraform/language/state/locking
Practice questions
What does the -lock-timeout option control?
- How long Terraform waits to acquire a state lock before failing
- How long a lock stays valid before it expires on its own
- How long a plan may run before it is cancelled
- How long the lock ID is shown in the terminal
Show answer
Answer: A. How long Terraform waits to acquire a state lock before failing
It sets how long Terraform waits to acquire a lock held by someone else; the default of 0s means it fails immediately. It does not expire existing locks or change lock IDs.
Which statements about state locking are correct?
- Terraform locks automatically for operations that could write state
- Locking happens only if the configured backend supports it
- -lock=false disables locking, but this is not recommended
- Every backend supports locking, so it is always active
- You must pass -lock=true on every command to enable it
Show answer
Answer: A. Terraform locks automatically for operations that could write state, B. Locking happens only if the configured backend supports it, C. -lock=false disables locking, but this is not recommended
Locking is automatic for write-capable operations, only happens when the backend supports it, and -lock=false is possible but discouraged. Not every backend locks, and a lock is not tied to a manual flag you must pass.
True or false: terraform force-unlock is a safe routine step to run whenever a plan seems slow to start.
Show answer
Answer: False
False. force-unlock should only release your own lock after automatic unlocking failed; releasing someone else's active lock can allow multiple writers and corrupt state.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
