TL;DR
State files hold your secrets in plain text. Client-side encryption, S3 native locking, and write-only arguments — the patterns that hold in late 2026.
Terraform state is the softest target in most infrastructure setups. It is a JSON document that maps your configuration to real resources — and it stores attribute values, including generated passwords and keys, in plain text. Anyone who can read the state file can read your infrastructure's secrets. The question is not whether to protect state, but which layer does the protecting: the backend, the client, or the decision to keep secrets out of the file entirely.
The short version: backend encryption and tight IAM are the floor, and every team should already have them. If you run OpenTofu, turn on client-side state encryption — it is the one control Terraform's open CLI does not offer. Keep secrets out of state with ephemeral resources and write-only arguments where the provider supports them. And move S3 locking off the DynamoDB table: native lockfiles have been generally available since Terraform 1.11. No single layer covers everything; the pattern that holds is all of them, sized to your team.
Why state is the softest target
Three properties make state different from every other file your pipeline touches. First, it concentrates secrets: database passwords, API tokens, private endpoints, and the full shape of your infrastructure sit in one JSON document. Our companion guide to state fundamentals puts it plainly — a leaked state file is a credential leak, and a deleted one orphans real resources.
Second, state travels. It lives in the backend, in CI runners, in plan files, in local copies on laptops, and in the occasional support bundle. Each copy is a new place the plaintext can leak from. Backend encryption at rest protects the copy in the bucket; it does nothing for the copy on a laptop or in a build cache.
Third, state is written by a tool that assumes the operator is trusted. Terraform and OpenTofu will happily write secrets to state if a resource returns them, and they will happily let two concurrent applies interleave writes unless locking is configured. The defaults assume a careful team. The patterns below are what careful looks like, as of late 2026 — versions current at writing are Terraform 1.17.1 (28 October 2026) and OpenTofu 1.13.9 (5 November 2026).
The options
Option 1: Backend protections only (the Terraform baseline)
The standard setup: state in a remote backend — S3 is the common one — with server-side encryption, versioning, and IAM that limits who can read the bucket. Add a bucket policy that denies unencrypted uploads and non-TLS access; our S3 bucket policy guide covers the exact statements.
This is the floor, and it is necessary. Versioning is your recovery path when state is corrupted or deleted. Encryption at rest (SSE-KMS) means a stolen disk or a bucket misconfiguration does not hand over plaintext on its own. Tight IAM means the read set is small.
But be honest about what it does not do. Anyone with s3:GetObject on the state key reads the secrets in plain text — backend encryption decrypts transparently for authorized readers. It does not protect the copies outside the bucket, and it does not stop a second apply from racing the first. If this is your only layer, your security posture is exactly as strong as your IAM policy, and IAM policies drift.
Pick this when you are on Terraform's open CLI and staying there, as the mandatory base. Do not pick it as the whole answer.
Option 2: Client-side state encryption (OpenTofu)
OpenTofu 1.7, released May 2024, added native client-side state and plan encryption — the first major feature where the fork diverged from Terraform, and one Terraform's open CLI still has no equivalent as of version 1.17.1. An encryption block in the terraform block names a key provider and a method, and OpenTofu encrypts the state with AES-GCM before the bytes ever reach the backend or a local file. A stolen terraform.tfstate is useless without the key.
terraform {
encryption {
key_provider "aws_kms" "team" {
kms_key_id = "arn:aws:kms:us-east-1:111122223333:key/abcd-1234"
region = "us-east-1"
key_spec = "AES_256"
}
method "aes_gcm" "kms" {
keys = key_provider.aws_kms.team
}
state {
method = method.aes_gcm.kms
enforced = true
}
plan {
method = method.aes_gcm.kms
enforced = true
}
}
}Key providers include PBKDF2 passphrases for solo work, AWS KMS, GCP KMS, and Azure Key Vault for teams, and OpenBao for self-hosted setups. The OpenTofu encryption docs are the primary reference. Two operational facts matter most. First, a passphrase provider means the passphrase becomes a secret you must manage — lose it and the state is unrecoverable, with no reset path; for anything shared, a KMS provider is the right call because access is gated by IAM and the key material lives in a managed service. Second, enabling encryption on an existing plaintext state needs an explicit migration step: OpenTofu refuses to read plaintext once encryption is on, so test the migration on a copy of the state before touching production.
Pick this when you run OpenTofu, or when you are choosing between the two and state secrecy is a deciding factor. The cost is real: you now manage an encryption key, and migrating back to Terraform requires decrypting first, since the encryption block is OpenTofu-only. For most teams that is a fair trade. Do not pick it as a substitute for IAM — encryption protects the file, IAM decides who may ask for it decrypted.
Option 3: Keep secrets out of state
The cheapest secret in state is the one that was never written there. Both projects now offer ways to keep sensitive values out of the file: ephemeral resources, which exist only for the duration of a run, and write-only arguments, which let you pass a sensitive value to a provider without Terraform storing it. Terraform supports ephemeral resources from 1.10 and write-only arguments from 1.11; OpenTofu supports both from 1.11.
This is the right layer for values that only need to exist briefly — a password generated for a single provisioning step, a token consumed during apply. Mark variables sensitive = true so they do not show in plan output, and never pass a secret through a terraform_remote_state data source, which hands the whole remote state — secrets included — to whoever can read the calling configuration.
The honest limit: this layer is partial. Providers still return attributes Terraform must store to manage resources — a database endpoint, a generated password on a resource that has no write-only argument yet. Ephemeral and write-only support depends on the provider implementing it for that resource, and coverage is still uneven. Treat this as reducing what lands in state, not eliminating it.
Pick this for every new module you write, on either tool. It costs almost nothing and it shrinks the blast radius of every other layer failing.
Option 4: Locking and least-privilege access
Locking stops two applies from racing and corrupting state. The S3 story changed in 2025: Terraform 1.10 (November 2024) introduced native S3 locking, and Terraform 1.11 (27 February 2025) made it generally available and deprecated the DynamoDB lock-table arguments. The backend now writes a .tflock object next to the state using S3 conditional writes — a feature AWS added in August 2024 — so no second service, table, or IAM policy is needed for locking.
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
}If you still run a DynamoDB lock table, migrate: enable use_lockfile alongside the table first so old and new runners both take a lock during the change, then remove the table once every runner has the new configuration. The migration walkthrough covers the sequence, including the .tflock IAM permissions (s3:GetObject, s3:PutObject, s3:DeleteObject on the lock key).
Locking is half the option; access is the other half. Split state per blast radius — one state file per environment or service, not one monolith for the whole estate — so a leaked read exposes one slice, not everything. Run applies from CI only, so the encryption key and the state are never on a laptop. And restrict state reads to the smallest set of humans and roles that genuinely need them; "everyone on the platform team" is not a least-privilege policy.
Pick this on every backend, on either tool, this week if you have not already. The DynamoDB table is the clearest stale-pattern signal in a Terraform setup in late 2026.
The comparison
| Option | What it protects | Terraform open CLI | OpenTofu | Cost |
|---|---|---|---|---|
| Backend protections | State at rest in the bucket; recovery via versioning | Yes — the floor | Yes — the floor | Low — bucket config + IAM |
| Client-side encryption | State and plan everywhere, including laptops and CI artifacts | No equivalent as of 1.17.1 | Yes, since 1.7 (May 2024) | Medium — a key to manage, a migration to test |
| Secrets out of state | Shrinks what secrets ever land in the file | Ephemeral 1.10+, write-only 1.11+ | Both since 1.11 | Low — module-level changes |
| Locking + least privilege | Concurrent-write corruption; blast radius of a read | Native lockfile 1.10+, GA 1.11 | Native S3 locking since 1.10 | Low — a backend flag and state splits |
Trade-offs
The trade-off nobody puts in the migration guide is key management. Client-side encryption moves the secret from the state file to the key, and the key now needs the care the state file used to get: rotation, access control, a recovery story. A KMS key with IAM-gated access and automatic rotation is manageable; a passphrase in a shared doc is a new incident waiting for a date. If your team cannot run KMS properly, encryption adds a failure mode — losing the key destroys the state — without the protection you think you bought.
The second trade-off is the fork decision. State encryption is, as of late 2026, the sharpest technical reason to pick OpenTofu over Terraform — a current comparison lists it as OpenTofu's standout exclusive, alongside provider for_each and early variable evaluation. But switching CLIs is an organizational decision, not a config change: CI images, wrapper scripts, module registries, and team habits all move. Do not switch tools for one feature without pricing the migration; do switch if the license question was already pushing you that way and encryption tips the balance.
The third trade-off is migration risk. Enabling encryption on a live state, or changing the locking mechanism under running CI, are both operations where a mistake costs a state file. Test on a copy, keep a versioned backup, and run the change when nobody else is applying. The versioning you enabled in Option 1 is what saves you here — which is why it comes first.
And the trade-off teams forget: encryption is not access control. An encrypted state that any CI job can decrypt is a state whose secrets are one compromised job away from exposure. Pair encryption with the least-privilege access in Option 4, and with policy checks — our Sentinel guide covers the policy-as-code half — or the encryption is theater.
Recommendation
For most teams in late 2026: start with the floor this week — S3 backend with SSE-KMS, versioning on, use_lockfile = true, no DynamoDB table, state split per environment, applies from CI only. That is a day's work and it closes the most common holes.
If you run OpenTofu, add client-side encryption next, with a KMS key provider — not a passphrase — and enforced = true on both state and plan. Test the migration on a copy first, and write down the key-recovery story before you enable it.
If you stay on Terraform, close the gap the other way: push ephemeral resources and write-only arguments into every new module, audit which resource attributes still carry secrets into state, and consider HCP Terraform if you want platform-level state protections without managing them yourself. Accept, explicitly, that state plaintext is readable by anyone with bucket read access — and make that read set tiny.
When not to use these patterns
Do not enable client-side encryption with a passphrase you have no recovery plan for. Losing the passphrase loses the state, and "we will remember it" is not a plan.
Do not split state so finely that cross-state references become spaghetti. One state per environment or service is a good rule; one per microservice with a web of terraform_remote_state reads is a new failure mode — every read hands the caller's secrets to the reader.
Do not treat encryption as a reason to relax IAM or skip policy checks. And do not build any of this for a throwaway experiment: local state on a laptop is fine for an afternoon's spike, as long as it never holds real credentials and never gets committed to git. The patterns above are for state that outlives the person who created it — which, in practice, is all production state.
What to do next
This week, run this checklist against your most important state file:
use_lockfile = true — no DynamoDB lock table on new setups, migration planned for old onesWhy This Matters
Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.
Trade-offs to Consider
Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.
When NOT to Use This
Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.
Decision Framework
Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.