Skip to content

Lesson 9 of 14 · 17m

Secrets and State: What Actually Lands on Disk

`sensitive` masks CLI output while passwords still land in state plaintext — ephemeral resources and write-only attributes are the mechanisms that keep them out.

Run terraform state show aws_db_instance.main on any project that sets password and the plaintext password prints to your terminal. sensitive = true on the variable masked it in plan output — the state file doesn't care. terraform.tfstate is a JSON document holding every attribute of everything Terraform manages, written to disk or a bucket, in cleartext.

The causal chain worth understanding: a secret enters through a variable → gets passed to a resource → the provider stores it as a resource attribute → Terraform records the attribute in state → anyone who can read the state bucket reads the secret. No exploit required — an S3 GetObject permission and jq. State files have leaked via misconfigured public buckets more than once, and each leak is every credential Terraform ever touched, at once.

What sensitive actually does

  • Masks the value in CLI output — plans show (sensitive value).
  • Propagates through references — a local or output built from a sensitive value is itself sensitive, and apply refuses to print it without sensitive = true on the output.
  • Does not keep the value out of state, out of plan.tfplan files, or out of terraform show output. It is a display flag, not an encryption mechanism.

The actual fixes, in order of when they work

Best: the secret never enters Terraform's memory at all. Ephemeral resources (Terraform 1.10+) exist only during a single run — the value is fetched, used, and never persisted to state or plan:

ephemeral "aws_secretsmanager_secret_version" "db" {
  secret_id = "prod/db/password"
}

resource "aws_db_instance" "main" {
  # password_wo is a write-only attribute (Terraform 1.11+)
  password_wo         = ephemeral.aws_secretsmanager_secret_version.db.secret_string
  password_wo_version = 3   # bump to rotate
}

Write-only attributes (*_wo, paired with a *_wo_version counter) let the provider use a value during apply without storing it in state — the database gets the password, the state file gets nothing. Rotate by bumping the version counter. Provider support is still spreading — check whether yours implements ephemeral/wo before relying on it.

Good: reference secrets stored elsewhere. A data source like data "aws_secretsmanager_secret_version" reads the value at plan time — but note the gotcha: data source reads do land in state (data sources persist; ephemeral resources don't). Use data sources when the secret reaching state is acceptable but the secret reaching git is not.

Acceptable: secrets live outside Terraform entirely. Vault, SSM Parameter Store, Secrets Manager as the system of record; the app reads them at runtime. Terraform creates the container (the Vault policy, the parameter, the IAM grant to read it), never the contents.

For repo-stored secrets: SOPS + age encrypts secrets.tfvars in-place — the repo gets ciphertext, terraform plan -var-file=<(sops -d secrets.tfvars) decrypts at run. Plaintext tfvars in git is the failure mode, full stop.

The state side of the bargain

Whatever secrets survive into state get protected there: KMS encryption at rest on the bucket, public access blocked, versioning on, least-privilege IAM, audit logging on reads. And the same rules apply to -out plan files and terraform show -json output — they're state by another name.

OpenTofu went further than Terraform here: version 1.7 added client-side state encryption, where the state file itself is encrypted before it leaves your machine. If your threat model treats the storage backend as hostile, that's a reason to evaluate the fork on its merits.

Field notes

  • terraform console evaluates expressions against state — aws_db_instance.main.password prints. Console access is state access.
  • Rotation cadence: if a secret ever hit state and you can't prove who read the file, rotate it.
  • The -detailed-exitcode exit-2 drift check runs plan — in CI, that plan output can contain sensitive values; gate the logs the same as the bucket.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.