State locking and safe recovery
Why a state lock exists
Suppose job A and job B both read the same state and start modifying objects. If each writes its own snapshot, the later write might hide changes made by the earlier job. A supported backend coordinates state writers through a lock. Terraform normally acquires that lock for operations that may write state and releases it when finished. A lock protects coordination around one state, not the availability of the application, not every remote object and not independent states that mistakenly manage the same object.
Respond to contention instead of bypassing it
Study this fictional event:
Fictional incident — not Terraform CLI outputState: catalog/devWriter: job-AJob A status: runningJob B status: waiting for state lockDecision: keep the lock; wait for job A to finish.Job B must not disable locking. Check the responsible run or operator and wait for orderly completion. For commands that support it, a bounded lock wait can handle ordinary short contention. In the private local fixture, this command demonstrates the option, but does not intentionally create a competing writer.
terraform plan -lock-timeout=30sThe timeout is an example, not a universal production value. If it expires, investigate rather than retrying indefinitely. A permission error, expired session or storage outage may also prevent locking; fix that cause without weakening access controls.
Understand backend-specific locks
S3 lockfile mode uses a separate lock object whose permissions must permit its lifecycle, including deletion of that lock object. Azure Blob Storage and GCS provide their own locking mechanisms; local locking uses system APIs. Do not infer the correct recovery procedure from the existence of a lock filename. The backend documentation and operational context decide how a lock can safely be released. Object-store versioning and encryption are separate controls, not locking mechanisms.
Investigate a suspected abandoned lock
Record a sanitized incident identifier, backend, state identity, lock ID, responsible operator and last known operation. Check the run controller and confirm the owning process has ended; an old timestamp alone is not proof. Prevent new writers during investigation. When your own failed operation left an orphaned lock and the backend supports manual release, follow the approved procedure with the exact ID returned by Terraform. The command syntax below is a reference, not a lab step.
terraform force-unlock LOCK_IDNever release another active writer’s lock or use forced confirmation to avoid a safety check. Local state cannot be unlocked by another process using this command. Manual unlock removes coordination, not resource changes; it does not recover an interrupted apply.
Recover state carefully after an interruption
An interrupted run may have created or modified objects before persisting its final state. A backend write failure can leave recovery state on local disk. Protect any recovery artifact as sensitive material. Establish a maintenance window, preserve current state and recoverable versions, compare the affected resource identities and actual authorized observations, and have the state owner review reconciliation. A previous snapshot may be inconsistent with infrastructure that changed since it was captured. Do not overwrite state merely because it is older or delete state to resolve a lock. Generate a new plan and inspect unexpected create, delete or replacement actions before approval.
Practice deciding when to stop
Use the synthetic incident cases in the lab directory. Decide whether to wait, fix access, escalate a suspected orphan, or investigate state recovery. There are no real lock files or remote state artifacts in this exercise. Success means choosing the appropriate safe action and explaining the evidence still needed, not forcing every operation to succeed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
State locking and safe recovery — knowledge check
Review backend selection and lock recovery.
What is the purpose of locking a supported state backend?
Another authorized job is actively writing state. What should you do?
A lock is several hours old. What does that prove?
What does force-unlock accomplish on a supporting backend?
After an interrupted apply and failed state write, what is the best next step?