Copy a Terraform root module to start a new one, trim the backend block, and forget to change key. A made-up but very typical example:
# app/backend.tf, copied from network/backend.tf
backend "s3" {
bucket = "acme-tf-state"
key = "terraform.tfstate" # same key as network/
region = "eu-west-1"
}
Both modules now write s3://acme-tf-state/terraform.tfstate. The next apply in one replaces the other's state, and the next plan in the other proposes to recreate everything it manages. Terraform never complains: it is a valid configuration.
What the docs actually say
- S3 state locking is opt-in.
use_lockfiledefaults tofalse. - Terraform introduced
use_lockfileas experimental in 1.10, made it generally available in 1.11, and deprecateddynamodb_table, which "will be removed in a future minor version". OpenTofu added native S3 locking in 1.10 and has no plans to deprecate DynamoDB locking. - Credentials hardcoded in a backend block or passed with
-backend-configend up in.terraform/and in plan files. - A backend block can not use variables or locals in Terraform (OpenTofu 1.8+ allows them).
- Without a backend, state is a local file. On a CI runner, it is gone after the job.
The usual suspects
- Two root modules, or two
.tfbackendfiles, resolving to the same bucket and key - S3 with neither
use_lockfilenordynamodb_table -
dynamodb_tablealone in a Terraform project -
access_keyor a password in a backend block or.tfbackendfile -
terraform applyin CI on a module with no backend -
-backend-config=prod.tfbackendpointing at a file nobody created - A committed
terraform.tfstate, or no.gitignoreentry for it
Checked in VS Code
I built StateLock, a free VS Code extension that works out where every root module and environment really stores its state (backend blocks merged with -backend-config files and flags from CI, Terragrunt remote_state, S3 workspace paths) and flags the risky ones. Ten rules, Quick Fixes (use_lockfile = true, encrypt = true, .gitignore lines), OpenTofu-aware, and an activity bar panel. On the example above it reports:
app and network both use s3://acme-tf-state/terraform.tfstate
s3://acme-tf-state/terraform.tfstate has no state locking
No network, no telemetry. Credentials are only ever shown masked, and state file contents are never read.
code --install-extension jaytankdev.statelock
Source (MIT): github.com/jay-tank/statelock
Full write-up (doc quotes, all ten rules, the manual fix and migration, FAQ): Terraform S3 backend state locking and key collisions
Top comments (1)
Two cheap guards next to the editor check. First, turn on versioning on the state bucket: when a copied key does get clobbered, the previous state is one aws s3api list-object-versions away instead of a rebuild with import. Second, a CI step that greps every backend block for bucket plus key and fails on duplicates catches the copy-paste case before any apply runs. Deriving the key from the directory path (for example env/app/terraform.tfstate) in a template makes it hard to forget in the first place.