Terraform's sensitive = true doesn't protect your secrets. Here's what does
Marking a Terraform output sensitive = true feels like protection. It isn't, at least not where it matters. Here's a two-minute proof, and what to do instead. Everything below is from a real run on Terraform 1.17.
Marking a Terraform output sensitive = true feels like protection. It isn't, at least not where it matters. Here's a two-minute proof, and what to do instead. Everything below is from a real run on Terraform 1.17.
The proof
A random database password, returned by an output marked sensitive:
resource "random_password" "db" {
length = 16
}
output "db_password" {
value = random_password.db.result
sensitive = true
}
After terraform apply, the CLI hides it:
$ terraform output
db_password = <sensitive>
Now read the state file:
$ jq -r '.resources[0].instances[0].attributes.result' terraform.tfstate
fqK5VEtF+v:imR-u
There's the password, in plain text.
What sensitive actually does
sensitive = true is a display setting. It:
- redacts the value in
terraform planandterraform applyoutput - shows
<sensitive>interraform output(thoughterraform output -raw db_passwordprints it) - spreads to everything derived from that value, so it doesn't leak through another output by accident
What it does not do:
- encrypt anything
- keep the value out of the state file
- keep it out of a saved plan file (
terraform plan -out=tfplan)
Terraform needs the real value to compare it on the next plan, so for a normal resource it stores it. That's by design.
How to actually protect secrets
1. Treat state as a secret
Because state can contain secrets, protect the whole file:
- keep it in a remote backend, never in Git (
*.tfstatebelongs in.gitignore) - turn on encryption at rest (for example
encrypt = trueon the S3 backend) - give read access only to the people and pipelines that need it
2. Pass secrets in, don't write them down
Never put secrets in .tf or .tfvars files. Set input variables from the environment instead:
$ export TF_VAR_db_password="..."
$ terraform plan
In CI, inject them from the pipeline's secret store.
3. Use ephemeral values (Terraform 1.10+)
Ephemeral resources generate or fetch a secret during a run only. It's never written to the plan or the state:
ephemeral "random_password" "db" {
length = 20
}
You can't put an ephemeral value somewhere that would be persisted, like a normal resource argument. Terraform rejects it with "Invalid use of ephemeral value". That's what makes it safe.
4. Use write-only arguments (Terraform 1.11+)
So how does an ephemeral value reach a real resource? Through write-only arguments: the provider receives the value, and Terraform never stores it:
resource "aws_db_instance" "main" {
engine = "postgres"
instance_class = "db.t3.micro"
allocated_storage = 5
username = "app"
password_wo = ephemeral.random_password.db.result
password_wo_version = 1 # bump to rotate the password
}
Because Terraform can't see the stored value, you bump password_wo_version to tell it you want to change the password.
5. Or keep secrets out of Terraform entirely
For many teams the cleanest answer is a secrets manager, such as HashiCorp Vault or your cloud's own, that applications read at runtime. Terraform then manages the access, not the secret itself.
Bonus: catch risky code before it's applied
Scanners like trivy (trivy config ., which now includes tfsec) and checkov (checkov -d .) read your Terraform code directly, with no credentials, and flag problems such as a security group that opens SSH to 0.0.0.0/0. Run them in CI on every pull request.
Summary
-
sensitive = truehides values in the terminal. It does not keep them out of state - Protect state: remote, encrypted, access-controlled, never in Git
- Pass secrets in with
TF_VAR_environment variables - Use ephemeral values and write-only arguments to keep secrets out of state completely
This post is based on episode 13 of Terraform from Zero, a free, hands-on Terraform course on YouTube that covers the whole Terraform Associate (004) exam, plus real-world troubleshooting.
- ▶ Full course: https://www.youtube.com/playlist?list=PLAp4RkKcTTLw
- 💻 Code for this episode: https://github.com/mrhr7/terraform-in-practice/tree/main/ep13_sensitive_data
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.