OpenTofu State Encryption
What State Is showed a private key sitting in plain text in terraform.tfstate. Backend encryption (S3 SSE, KMS) protects the file at rest in the bucket, but anyone who can read the object (a too-broad IAM policy, a leaked CI artifact, a state pull on a laptop) reads every secret. OpenTofu 1.7+ can encrypt state and plan files on the client side, before they leave the CLI, so the backend only ever stores ciphertext. Terraform has no equivalent.
The building blocks
terraform {
encryption {
key_provider "pbkdf2" "main" { # where the key comes from
passphrase = var.state_passphrase # early evaluation: variables are allowed here
}
method "aes_gcm" "main" { # how data is encrypted
keys = key_provider.pbkdf2.main
}
state { # what is encrypted
method = method.aes_gcm.main
enforced = true # refuse to write unencrypted state
}
plan {
method = method.aes_gcm.main
enforced = true
}
}
}| Key provider | Key comes from |
|---|---|
pbkdf2 |
a passphrase (at least 16 characters), stretched with PBKDF2 |
aws_kms |
an AWS KMS key (a data key per write, wrapped by KMS) |
gcp_kms, openbao |
Google Cloud KMS, OpenBao (the open-source Vault fork) |
external (1.10+) |
any program you provide, e.g. a wrapper around your secret manager |
Encrypting, for real
mkdir enc && cd enc
cat > main.tf <<'HCL'
terraform {
required_providers {
tls = { source = "hashicorp/tls", version = "~> 4.0" }
}
encryption {
key_provider "pbkdf2" "main" {
passphrase = var.state_passphrase
}
method "aes_gcm" "main" {
keys = key_provider.pbkdf2.main
}
state {
method = method.aes_gcm.main
enforced = true
}
plan {
method = method.aes_gcm.main
enforced = true
}
}
}
variable "state_passphrase" {
type = string
sensitive = true
}
resource "tls_private_key" "deploy" {
algorithm = "ED25519"
}
HCL
export TF_VAR_state_passphrase="correct-horse-battery-staple"
tofu init >/dev/null
tofu apply -auto-approve | tail -1
echo "--- what the backend stores"
jq -r 'keys[]' terraform.tfstate
grep -c 'PRIVATE KEY' terraform.tfstate
echo "--- OpenTofu with the passphrase reads it normally"
tofu state list
echo "--- a wrong passphrase"
TF_VAR_state_passphrase="not-the-right-passphrase" tofu state list 2>&1 | grep -E 'Error|failed'
echo "--- plan files are encrypted too"
tofu plan -out=tfplan >/dev/null
tofu show tfplan | tail -1
unzip -l tfplan >/dev/null 2>&1 && echo "plan is a readable zip" || echo "plan is not a readable zip"Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
--- what the backend stores
encrypted_data
encryption_version
lineage
meta
serial
0
--- OpenTofu with the passphrase reads it normally
tls_private_key.deploy
--- a wrong passphrase
│ Error: Error refreshing the state
│ Failed to load state: decryption failed for all provided methods
│ attempted decryption failed for state: decryption failed: cipher: message
│ authentication failed
--- plan files are encrypted too
No changes. Your infrastructure matches the configuration.
plan is not a readable zip
The state file now holds encrypted_data and key-derivation metadata (meta: the salt and iteration count, not the key) instead of resources; grep finds no private key. Normally a saved plan is a zip archive anyone can unpack; encrypted, it is opaque. And Terraform can't use any of it: it rejects the encryption block (Blocks of type "encryption" are not expected here), so put the block in a .tofu file if the module must stay Terraform-compatible (OpenTofu).
Adopting it on an existing state
You can't switch on enforced encryption for a state that is still plain: OpenTofu must be told it may read the old format once. The unencrypted method plus a fallback does that; after one apply the state is encrypted and the fallback can go:
mkdir adopt && cd adopt
cat > main.tf <<'HCL'
resource "terraform_data" "app" {
input = "v1"
}
HCL
tofu init >/dev/null && tofu apply -auto-approve >/dev/null
jq -r 'has("resources")' terraform.tfstate
cat > encryption.tofu <<'HCL'
variable "state_passphrase" {
type = string
sensitive = true
}
terraform {
encryption {
key_provider "pbkdf2" "main" {
passphrase = var.state_passphrase
}
method "aes_gcm" "main" {
keys = key_provider.pbkdf2.main
}
method "unencrypted" "migrate" {}
state {
method = method.aes_gcm.main
fallback {
method = method.unencrypted.migrate # may still READ plain state; always WRITES encrypted
}
}
}
}
HCL
export TF_VAR_state_passphrase="correct-horse-battery-staple"
tofu apply -auto-approve | tail -1
jq -r 'has("resources"), has("encrypted_data")' terraform.tfstatetrue
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
false
true
The same fallback mechanism rotates keys: make the new key provider the primary method and the old one the fallback, apply once (the state is re-encrypted with the new key), then remove the fallback.
In practice
Use a KMS key provider in teams, not a passphrase: access to state then follows IAM permissions on the KMS key, and nobody handles raw secrets.
hclkey_provider "aws_kms" "main" { kms_key_id = "alias/linkly-tfstate" region = "eu-west-1" key_spec = "AES_256" }Losing the key loses the state. Back up the passphrase or keep the KMS key protected from deletion (KMS keys have a mandatory waiting period before deletion; keep it long).
Configuration can also come from the
TF_ENCRYPTIONenvironment variable (same HCL), handy to keep encryption settings out of the repository or to set them per pipeline.terraform_remote_statereads of an encrypted state need the key too: configure it underremote_state_data_sourcesin theencryptionblock.Encryption complements, not replaces, the backend's own protection: keep S3 encryption at rest, versioning and least-privilege access.
Test yourself
What does OpenTofu's client-side state encryption protect against that S3 server-side encryption doesn't?
- Disk failure in AWS
- Anyone with read access to the state object (or a copy of it) reading the secrets inside
- Concurrent applies
- Drift
How do you switch an existing plain-text state to encrypted?
- Delete it and re-import everything
- Add the encryption block with a
fallbackto theunencryptedmethod, apply once, then remove the fallback tofu state encrypt- Set
encrypt = truein the S3 backend
Can Terraform 1.17 read a state written with OpenTofu state encryption?
- Yes
- No: Terraform doesn't support the encryption block or encrypted state
- Only with -force
- Only plan files