Bitzu

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

Single choice

What does the -lock-timeout option control?

  1. How long Terraform waits to acquire a state lock before failing
  2. How long a lock stays valid before it expires on its own
  3. How long a plan may run before it is cancelled
  4. 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.

Select all that apply

Which statements about state locking are correct?

  1. Terraform locks automatically for operations that could write state
  2. Locking happens only if the configured backend supports it
  3. -lock=false disables locking, but this is not recommended
  4. Every backend supports locking, so it is always active
  5. 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

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.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module