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
localoroutputbuilt from a sensitive value is itself sensitive, andapplyrefuses to print it withoutsensitive = trueon the output. - Does not keep the value out of state, out of
plan.tfplanfiles, or out ofterraform showoutput. 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 consoleevaluates expressions against state —aws_db_instance.main.passwordprints. 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-exitcodeexit-2 drift check runsplan— in CI, that plan output can contain sensitive values; gate the logs the same as the bucket.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
What is Infrastructure as a Service? The Layer That Makes Everything Else Possible
Infrastructure as a Service gives you virtualized servers, storage, and networking on demand without owning any hardware. You rent compute and own everything that runs on it. Here is what IaaS actually is, what it demands from you.
What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"
Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.
The World now is Serverless and Container based
A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,