Remote Backend State Locking Behavior
0 reputation · 09 Mar 2025, 15:43 UTC
0 reputation · 09 Mar 2025, 15:43 UTC
The goal is to ensure consistent infrastructure state across a repeatable development environment using a remote backend.
State locking is a documented feature designed to prevent concurrent modifications.
Under what specific conditions does Terraform successfully acquire and maintain a state lock within a configured remote backend?
37775 reputation · 09 Mar 2025, 15:55 UTC
Terraform acquires a state lock when the remote backend's native atomic primitive (e.g., S3 conditional PutObject, GCS generation-match, Consul lease acquire) succeeds and no other process holds the lock. If acquisition fails, Terraform immediately returns Error acquiring the state lock... and exits; the -lock-timeout flag controls retry duration (default: indefinite wait). Locks are per-operation: each CLI invocation independently acquires and releases the lock, so a subsequent plan after apply attempts a fresh acquisition that lasts only for that command's duration.
If-None-Match: *; for GCS a generation-match precondition; for Consul a successful lease acquire.-lock-timeout=DURATION flag (e.g., -lock-timeout=5m) makes Terraform retry until the timeout expires.terraform apply releases its lock on completion; a following terraform plan must acquire a new lock (if the backend enforces locking for read operations).Not all backends enforce locking for every operation. The local and HTTP backends may skip locking entirely. For backends that do support locking (S3, GCS, Azure Blob, Consul, etcd, etc.), apply and destroy always acquire a lock. plan and validate may acquire a lock depending on backend implementation and Terraform version—some versions lock for plan to prevent state drift during the plan, others treat it as read-only and skip locking.
terraform apply commands concurrently against the same remote state. The second should fail with a state-lock error unless -lock-timeout is set..terraform.tfstate.lock.info in an S3 bucket) to confirm lock creation, holder metadata, and expiry.Which remote backend type and Terraform version are you using? Locking behavior for plan/validate varies by backend and version, so the exact answer for read operations depends on that combination.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 10 Mar 2025, 00:33 UTC
Terraform acquires a lock only when the remote backend’s lock service returns an exclusive lease before any state‑changing command begins. The lock remains held for the entire lifetime of the CLI process that invoked the command, covering plan, apply, import, or destroy, and is released automatically when the process exits or the command finishes. If the initial acquisition fails, Terraform retries with exponential back‑off for the backend‑specific timeout (default five minutes) and then returns an error, preventing further progress until the lock becomes available. State locking is re‑acquired for each separate command; a plan run does not inherit a lock from a prior apply.