Remote State and Locking in OpenTofu / Terraform
Why local tfstate breaks in teams, how to configure S3 or GCS remote backends with state locking, and how OpenTofu encrypts state client-side.
TL;DR
Never store terraform.tfstate on a laptop or in Git. Put state in a remote backend (S3 or GCS) with versioning and locking (use_lockfile = true on S3, or DynamoDB for legacy setups), and enable OpenTofu client-side state encryption so secrets in state stay unreadable at rest.
Why it exists
OpenTofu and Terraform track every managed resource ID in a JSON state file. If Alice runs tofu apply from her laptop and Bob runs it from CI using separate or stale local files, they silently destroy or duplicate cloud resources—and committing .tfstate to Git leaks database passwords and private keys in plaintext.
Mental model
A remote backend splits state into two guarantees:
- Shared durable storage: S3 or GCS with bucket versioning so any CI runner reads the exact latest state and can recover from accidental state corruption.
- Mutual exclusion (locking): Before
planorapplywrites, OpenTofu acquires a lock (<key>.tflockin S3 or a DynamoDB item). A second runner fails fast instead of racing.
Do it
Configure an S3 remote backend with native S3 lockfile (use_lockfile = true) and OpenTofu client-side state encryption (not run—requires an S3 bucket and KMS key):
# backend.tf (not run; requires AWS bucket & KMS key)
terraform {
required_version = ">= 1.10.0"
backend "s3" {
bucket = "acme-iac-state-prod"
key = "network/terraform.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
encryption {
key_provider "aws_kms" "state" {
kms_key_id = "alias/tofu-state-key"
region = "us-east-1"
}
method "aes_gcm" "encrypted" {
keys = key_provider.aws_kms.state
}
state {
method = method.aes_gcm.encrypted
enforced = true
}
}
}For GCS, locking is built into the bucket automatically via object generation preconditions (backend "gcs" { bucket = "..." prefix = "..." }).
Gotchas
- No variables in
backend {}: standard backend blocks cannot referencevar.*orlocal.*—pass environment-specific keys via-backend-config=prod.s3.tfbackend(or separate root directories). - Stale locks after a killed CI job: if a runner is
SIGKILLed mid-apply, the lock stays held. Verify no run is actually active before runningtofu force-unlock <LOCK_ID>. - DynamoDB deprecation path: older guides require a
dynamodb_tablefor S3 locking; modern OpenTofu (1.10+) and Terraform (1.10+) support native S3 conditional-write locking withuse_lockfile = true. - Bucket versioning is non-negotiable: encryption protects confidentiality, but S3 versioning is what lets you roll back a corrupted state file.
Production checklist
- Enable bucket versioning and block all public access on the state bucket
- Enable state locking (
use_lockfile = truefor S3, native for GCS) - Enable OpenTofu client-side
encryptionwith KMS (enforced = true) - Restrict state bucket IAM access to CI roles and break-glass admins only
- Split state by blast radius (network, database, app) rather than one giant state file
Check yourself
Check Yourself
1. Why does OpenTofu client-side state encryption matter if S3 already has server-side encryption (SSE-S3/SSE-KMS) enabled?
2. A CI job was cancelled mid-apply and the next run fails with 'Error acquiring the state lock'. What should you do before running force-unlock?
Sources
Related Notes
How Plan and Apply Work: State, Refresh, Diff
How OpenTofu combines .tf configuration and state to refresh real infrastructure, compute a diff, preview a plan, and apply changes.
IaC Modules: When and How to Extract Them
Structure reusable OpenTofu/Terraform modules with clean inputs, outputs, and semantic versioning—and know when a module is premature abstraction.