004 · Domain 6: Terraform state management · 13% of the exam
Task 6.2: Describe state locking
Locking that stops two runs from writing the same state: which operations lock, backends that support it, -lock-timeout, and force-unlock as a last resort.
Study it
State locking: why concurrent writers corrupt state
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A team using a backend that supports locking asks whether they must acquire the state lock themselves before running terraform apply. What is the answer?
AYes; they must run terraform force-unlock -lock before each apply
BYes; they must add a lock block to the terraform block of the configuration
CNo, because state locking only applies to terraform destroy operations
DNo; it is locked automatically for operations that could write it
Show the answer and why
AYes; they must run terraform force-unlock -lock before each apply
Incorrect
force-unlock removes a lock; nobody runs it to take one.
BYes; they must add a lock block to the terraform block of the configuration
Incorrect
There is no lock block; locking is a backend feature used automatically.
CNo, because state locking only applies to terraform destroy operations
Incorrect
Every operation that could write state is locked, not just destroy.
DNo; it is locked automatically for operations that could write it
Correct
State locking happens automatically on all operations that could write state, without any message unless acquiring the lock takes a while.
Locking is automatic and silent; you notice it only when someone else holds the lock.
A CI job was killed during an apply, and every later run fails because the state is still locked. The team has confirmed that nothing else is running against this state. What should they do?
ARun terraform force-unlock with the lock ID shown in the error
BWait for the lock to expire, which happens one hour after it was taken
CDelete the state file in the backend and run apply again
DRun terraform init -reconfigure, which clears any locks on the state
Show the answer and why
ARun terraform force-unlock with the lock ID shown in the error
Correct
force-unlock removes a lock by its unique ID; use it only for your own lock when automatic unlocking failed.
BWait for the lock to expire, which happens one hour after it was taken
Incorrect
Terraform state locks are not released on a timer; a stale lock stays until it is removed.
CDelete the state file in the backend and run apply again
Incorrect
Deleting state throws away Terraform's record of what it manages, which is far worse than a stale lock and never the way to clear one.
DRun terraform init -reconfigure, which clears any locks on the state
Incorrect
-reconfigure changes backend settings handling; it does not release locks.
force-unlock is a last resort for a lock you know is abandoned; the lock ID makes sure you remove the right one.