Keep OpenTofu state in a versioned, encrypted S3 bucket with a lock, so the whole team works from one copy and two runs cannot overwrite each other.
tofu apply
State lock
S3 state bucket
Old versions kept
How it works
Why state should not live on a laptop
OpenTofu records what it created in a state file. If that file sits on one person's machine, nobody else can make a change safely, and a lost laptop means OpenTofu no longer knows what it manages. A shared bucket gives everyone, and your deploy pipeline, the same copy.
Create the bucket
The bucket blocks public access, encrypts objects at rest with a KMS key that you own and rejects any request that does not use TLS. Versioning is on, so if an apply goes wrong you can restore the previous state file. Old versions are deleted after 90 days to keep the bucket small.
Add the lock
Two applies running at once can each write a state file that is missing the other's changes. The DynamoDB table holds a lock while a run is in progress, so the second run waits or stops. On OpenTofu 1.10 or later you can skip the table and set use_lockfile = true in the backend block.
Apply this once, with local state
The bucket cannot store its own state before it exists, so apply this configuration first with state on disk. The mistake people make is deleting that small local state file afterwards. Keep it in a safe place, or add the backend block to this configuration too and run tofu init -migrate-state.
Point your other configurations at it
Paste the backend block from the top of main.tf into each configuration, set kms_key_id to the kms_key_arn output, and give each one its own key, such as prod/network/terraform.tfstate. Run tofu init and answer yes when it offers to copy existing state. Sharing one key between two configurations makes each overwrite the other.
Limit who can read it
State files can contain generated passwords and other sensitive values in plain text. Give read and write access on the bucket only to the people and deploy roles that run OpenTofu. They also need kms:GenerateDataKey and kms:Decrypt on the state key, so a role without the key cannot read state even if it can reach the bucket.
What you set
state_bucket_name
Globally unique name for the state bucket For example acme-tofu-state
region
Region that holds the bucket and lock table For example us-east-1
create_lock_table
Set to false when you lock with use_lockfile on OpenTofu 1.10 or later For example true
What it costs to run
About 1 dollar a month: the KMS key is 1 dollar, state files are small and the lock table bills per request. Estimate.
The configuration
# Remote state for OpenTofu: an S3 bucket for the state file and a lock so two
# runs cannot write at the same time.
#
# What this creates:
# - a KMS key of your own that encrypts the state files
# - a private, versioned S3 bucket, encrypted with that key, that refuses
# requests without TLS
# - a DynamoDB table used only as the state lock
#
# Apply this once with local state, then paste the backend block below into
# every other configuration and run `tofu init`. Give each configuration its
# own key.
#
# terraform {
# backend "s3" {
# bucket = "acme-tofu-state"
# key = "prod/network/terraform.tfstate"
# region = "us-east-1"
# encrypt = true
# kms_key_id = "arn:aws:kms:us-east-1:111122223333:key/<key id>"
# dynamodb_table = "acme-tofu-state-lock"
# }
# }
#
# Take kms_key_id from the kms_key_arn output. Without that line the backend
# asks S3 for its own key (AES256) on every write, and yours is not used.
#
# On OpenTofu 1.10 or later you can lock with a file in the bucket instead:
# replace the dynamodb_table line with `use_lockfile = true` and set
# create_lock_table to false.
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.region
}
variable "region" {
description = "AWS region that holds the state bucket."
type = string
default = "us-east-1"
}
variable "state_bucket_name" {
description = "Name of the state bucket. Bucket names are global, so include your company or account name."
type = string
}
variable "create_lock_table" {
description = "Create the DynamoDB lock table. Set to false if you lock with use_lockfile on OpenTofu 1.10 or later."
type = bool
default = true
}
variable "kms_deletion_window_days" {
description = "Days AWS waits before it deletes the state key after a destroy. Between 7 and 30."
type = number
default = 30
}
variable "noncurrent_version_days" {
description = "Days to keep old versions of a state file before they are deleted."
type = number
default = 90
}
# A key you own, so who can read state is decided by key permissions as well
# as bucket permissions, and every use of the key is recorded in CloudTrail.
resource "aws_kms_key" "state" {
description = "Encrypts OpenTofu state in ${var.state_bucket_name}"
enable_key_rotation = true
# A deleted key makes every state file unreadable for good, so use the
# longest waiting period AWS allows. You can cancel a deletion during it.
deletion_window_in_days = var.kms_deletion_window_days
}
# A readable name for the key in the console and in IAM policies.
resource "aws_kms_alias" "state" {
name = "alias/${var.state_bucket_name}"
target_key_id = aws_kms_key.state.key_id
}
resource "aws_s3_bucket" "state" {
bucket = var.state_bucket_name
# Losing this bucket loses the record of everything OpenTofu manages.
# To tear down a test, remove this block first.
lifecycle {
prevent_destroy = true
}
}
# ACLs off: access is decided by IAM and the bucket policy only.
resource "aws_s3_bucket_ownership_controls" "state" {
bucket = aws_s3_bucket.state.id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
resource "aws_s3_bucket_public_access_block" "state" {
bucket = aws_s3_bucket.state.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# Versioning is the undo button: a bad apply or a corrupted state file can be
# rolled back to the previous version.
resource "aws_s3_bucket_versioning" "state" {
bucket = aws_s3_bucket.state.id
versioning_configuration {
status = "Enabled"
}
}
# State files hold resource attributes in plain text, sometimes including
# generated passwords, so they are encrypted at rest with the key above.
resource "aws_s3_bucket_server_side_encryption_configuration" "state" {
bucket = aws_s3_bucket.state.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
kms_master_key_id = aws_kms_key.state.arn
}
# One data key per bucket instead of one KMS request per object, which
# keeps the KMS bill at a few cents.
bucket_key_enabled = true
}
}
resource "aws_s3_bucket_lifecycle_configuration" "state" {
bucket = aws_s3_bucket.state.id
# Lifecycle rules for old versions only work once versioning is on.
depends_on = [aws_s3_bucket_versioning.state]
rule {
id = "expire-old-state-versions"
status = "Enabled"
filter {}
noncurrent_version_expiration {
noncurrent_days = var.noncurrent_version_days
}
}
}
data "aws_iam_policy_document" "tls_only" {
statement {
sid = "DenyRequestsWithoutTls"
effect = "Deny"
actions = ["s3:*"]
resources = [
aws_s3_bucket.state.arn,
"${aws_s3_bucket.state.arn}/*",
]
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}
resource "aws_s3_bucket_policy" "state" {
bucket = aws_s3_bucket.state.id
policy = data.aws_iam_policy_document.tls_only.json
# Applying both at once can fail, so the public access block goes first.
depends_on = [aws_s3_bucket_public_access_block.state]
}
# The lock table holds one small item while a run is in progress, so on-demand
# billing costs close to nothing.
resource "aws_dynamodb_table" "lock" {
count = var.create_lock_table ? 1 : 0
name = "${var.state_bucket_name}-lock"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
server_side_encryption {
enabled = true
}
}
output "state_bucket" {
description = "Put this in the backend block as bucket."
value = aws_s3_bucket.state.id
}
output "kms_key_arn" {
description = "Put this in the backend block as kms_key_id."
value = aws_kms_key.state.arn
}
output "lock_table" {
description = "Put this in the backend block as dynamodb_table. Null when create_lock_table is false."
value = var.create_lock_table ? aws_dynamodb_table.lock[0].name : null
}
Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.
Written by DevOps Hive. Last updated 2026-11-27.
We use analytics cookies and session recordings to find and fix what is confusing on this site. Privacy