DEV Community

Cover image for Two Terraform root modules, one S3 key: state locking and backend collisions
TANK JAY
TANK JAY

Posted on Originally published at jaytank.hashnode.dev

Two Terraform root modules, one S3 key: state locking and backend collisions

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"
}
Enter fullscreen mode Exit fullscreen mode

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

  1. S3 state locking is opt-in. use_lockfile defaults to false.
  2. Terraform introduced use_lockfile as experimental in 1.10, made it generally available in 1.11, and deprecated dynamodb_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.
  3. Credentials hardcoded in a backend block or passed with -backend-config end up in .terraform/ and in plan files.
  4. A backend block can not use variables or locals in Terraform (OpenTofu 1.8+ allows them).
  5. 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 .tfbackend files, resolving to the same bucket and key
  • S3 with neither use_lockfile nor dynamodb_table
  • dynamodb_table alone in a Terraform project
  • access_key or a password in a backend block or .tfbackend file
  • terraform apply in CI on a module with no backend
  • -backend-config=prod.tfbackend pointing at a file nobody created
  • A committed terraform.tfstate, or no .gitignore entry 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
Enter fullscreen mode Exit fullscreen mode

No network, no telemetry. Credentials are only ever shown masked, and state file contents are never read.

code --install-extension jaytankdev.statelock
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
hritik_chaudhary_5db0056f profile image
Hritik Chaudhary •

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.