Use it when configuring, checking or troubleshooting State Locking in Terraform / OpenTofu.
State Locking · Terraform / OpenTofu
Terraform/OpenTofu state locking prevents concurrent state-writing operations when the selected backend supports a locking mechanism.
1. Locking prevents concurrent modification.
2. Backend-specific mechanisms.
3. Don't disable locking (-lock=false).
4. Force-unlock only after verifying.
5. CI concurrency controls.
run → lock → change → unlock
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Operational evidence path Verify boundary
Healthy evidence: Writes acquire the expected lock and stale-lock recovery is performed only after proving no active writer remains.
Step 2 Verification path Verify boundary
For Terraform / OpenTofu → State Locking, the evidence must connect the control-plane result to the provider-owned infrastructure objects plus authoritative IaC state representing them and explain any gap between desired, controller-reported and consumer-visible state.
Step 3 Failure modes and next checks Narrow the fault
Suspicious evidence: Locking is unsupported/disabled, force-unlock is used without ownership evidence, or separate state files concurrently manage overlapping objects.
Step 4 Common mistakes Narrow the fault
force-unlockwhile a run is active.
Step 5 Where it fails Narrow the fault
Failure analysis for Terraform / OpenTofu → State Locking should follow the path through the components: configuration/variables → graph/plan → state lock → provider API → infrastructure mutation → state update → refresh/target read-back.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Lock success ≠ exclusive infrastructure ownership; out-of-band API changes and other states can still modify the same remote systems.
- Terraform/OpenTofu command success ≠ target workload health; provider operations and application acceptance are separate proof boundaries.
- The same repository ≠ same run when binary, provider/module versions, backend/workspace, variables, credentials or lock selections differ.
- Treat Terraform and OpenTofu as separate implementations: verify behavior against the pinned tool and dependency versions rather than assuming interchangeability.
- Lock table/lockfile/blob lease configured — locking
terraform plan (concurrent attempt shows lock)— testterraform force-unlock <id>— emergency only
ReferenceWhy it matters1 section · depth on demand
In depth
State locking prevents two Terraform runs from modifying the same state at the same time, which would corrupt it or create conflicting resources. Most remote backends lock automatically during plan and apply.
| Backend | Locking |
|---|---|
| S3 | Native lock file (use_lockfile = true) or DynamoDB table |
| Azure Blob | Blob lease |
| GCS | Native |
| Terraform Cloud/Enterprise, HTTP (GitLab) | Native |
| Local | Only on the same machine |
terraform plan -lock-timeout=5m # wait for a lock instead of failing immediately
terraform force-unlock <LOCK_ID> # only if the holder is certainly gone
A stale lock after a crashed CI job is common; confirm no run is active before force-unlocking.
ReferenceImportant terms & boundaries2 sections · depth on demand
What this shows and what it does not
Lock success ≠ exclusive infrastructure ownership; out-of-band API changes and other states can still modify the same remote systems.
Mechanics
Terraform / OpenTofu → State Locking is owned at runtime by the Terraform/OpenTofu CLI/runtime, provider plugins and remote APIs/state backend that evaluate and apply the graph. The linked authority for bounded mechanics is OpenTofu Docs — State Locking; documentation support is separate from observed lab/production Evidence.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Terraform / OpenTofu > State Locking. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://opentofu.org/docs/language/state/locking/ · confidence 94%Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
State Locking IaC proof-boundary map
Fast definition/state/plan/execution/target-proof boundaries for State Locking.
| Boundary | Healthy evidence | Suspicious evidence |
|---|---|---|
| Definition / inputs | Exact revision, toolchain and effective inputs are reproducible | Different runner/version/input context changes evaluation |
| State / planning | Correct backend/state/lock, fresh reads and reviewed graph/plan | Wrong state/workspace, stale reads, unsafe replacement/destroy or missing lock |
| Execution | Expected provider/API operations complete and state persists | Partial apply, auth/API error, ambiguous retry or failed state write |
| Target proof | Independent read-back plus fresh plan confirm intended convergence | Engine success treated as workload acceptance or drift remains unexplained |