Skip to Content
Mengty Learns
IaC

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.

how-tointermediateTested with opentofu 1.13 (docs only)Updated Nov 27, 2026

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:

  1. 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.
  2. Mutual exclusion (locking): Before plan or apply writes, OpenTofu acquires a lock (<key>.tflock in 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 reference var.* or local.*—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 running tofu force-unlock <LOCK_ID>.
  • DynamoDB deprecation path: older guides require a dynamodb_table for S3 locking; modern OpenTofu (1.10+) and Terraform (1.10+) support native S3 conditional-write locking with use_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 = true for S3, native for GCS)
  • Enable OpenTofu client-side encryption with 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