Domain 6 of 8 · Chapter 2 of 4

Describe state locking

Unlock the complete study guide + 1,040 practice questions across 16 full exams.

Bundled into the existing HashiCorp Certified: Terraform Associate premium course — no separate purchase.

14-day money-back guarantee — no questions asked.

Included in this chapter:

  • Why Terraform locks state
  • Backends decide whether locking exists
  • Controlling the lock: -lock and -lock-timeout
  • Recovering a stuck lock with force-unlock
  • Exam pattern recognition

State-lock control surface

Control-lock=true (default)-lock-timeout=DURATION-lock=falseforce-unlock LOCK_ID
What it doesAcquires the lock, fails fast if it is heldRetries acquisition for the duration, then errorsSkips the lock for the commandManually releases an existing lock
KindDefault behaviorFlag on plan, apply, and similarFlag on plan, apply, and similarSeparate command
Concurrency safetyProtectedProtected, waits for the holderUnprotectedRestores locking after a stale lock
Typical useEveryday runsOverlapping CI or queued runsBackend lacks locking, or a solo one-offClearing a stale lock after a crash
Main riskNoneLong wait if the holder is stuckConcurrent writes corrupt stateReleasing an active lock corrupts state

Decision tree

Lock already held? Default lock, run proceeds Another run active? Can you wait for it? force-unlock LOCK_ID -lock-timeout=DURATION Never unlock an active lock -lock=false only if the backend cannot lock No Yes active stale Yes No

Cheat sheet

  • Locking happens automatically on state-writing operations
  • Terraform halts if it cannot acquire the lock
  • Locking support depends on the backend
  • -lock=false disables state locking
  • -lock-timeout retries lock acquisition for a duration
  • terraform force-unlock needs the lock ID
  • -force skips the force-unlock confirmation
  • Only force-unlock a lock you own

Unlock with Premium — includes all practice exams and the complete study guide.

References

  1. State: Locking
  2. Backend Type: s3
  3. Command: plan
  4. Command: force-unlock