Lesson 7 of 9 · 27m
Secrets in Terraform Without Leaking Them
Understand what Terraform can and cannot protect, how secrets enter state and plans, and patterns for secret references, rotation, and external stores.
Terraform has an uncomfortable secret: it is excellent at configuring secret infrastructure and mediocre at being a secret store. Teams learn this after a database password appears in CI logs, a private key lands in state, or a 'sensitive' output turns up in a pull request comment. Sensitive is a display control, not a security boundary.
Assume Terraform state can contain secrets
Provider resources often return secret material or secret-derived attributes. Terraform must store enough data to manage future diffs:
resource "random_password" "database" {
length = 32
special = true
}
resource "aws_db_instance" "main" {
username = "app"
password = random_password.database.result
}This password will be in state even if the value never prints in a plan. Encrypt state at rest, restrict reads, version it, and treat state snapshots as secret-bearing production backups.
Mark outputs sensitive, but do not stop there
output "database_password" {
value = random_password.database.result
sensitive = true
}This suppresses ordinary CLI display. It does not remove the value from state, does not prevent a caller from using it, and does not magically protect CI artifacts. If you do not need an output, do not create one.
Prefer secret references over secret values
The strongest pattern is to create or rotate secrets in a dedicated secret manager and pass references to applications:
resource "aws_secretsmanager_secret" "database" {
name = "prod/billing/database"
}
resource "aws_secretsmanager_secret_version" "database" {
secret_id = aws_secretsmanager_secret.database.id
secret_string = jsonencode({
username = "billing"
password = random_password.database.result
})
}Even here, Terraform state may contain the secret version payload. A stronger design is often rotation outside Terraform, with Terraform wiring IAM policies, rotation Lambdas, endpoints, and consumer references rather than the current secret value.
Inputs have lifecycle rules
Variables can be supplied through environment variables:
export TF_VAR_database_password="$(aws secretsmanager get-secret-value --secret-id prod/billing/bootstrap --query SecretString --output text)"Never write them into committed terraform.tfvars. Ephemeral values in memory are better than files, but command history, process lists, CI masking behavior, and shell redirection all matter. Use CI secret injection and avoid echoing values into logs.
Plans and state are artifacts with access controls
Saved plans may include secret values, particularly when a resource changes:
terraform plan -out=tfplan
terraform show -json tfplan > tfplan.jsonStore these artifacts with the same controls as state. Pull request automation should post redacted summaries, not raw JSON diffs. A public repository with private infrastructure values is still a leak even if the repository is not the production system.
Rotation is an ownership decision
Decide who rotates each class of secret: Terraform, the application runtime, the cloud secret manager, or an external security platform. If Terraform rotates on every apply, you create unnecessary replacement and coordination risk. If nobody rotates because Terraform created it once, you have built a permanent credential generator.
Field notes
sensitive = truereduces accidental display; it is not encryption and not authorization.- Check
.terraformdirectories, crash logs, plan files, and state backups in secret scanning. - Prefer dynamic credentials such as workload identity, instance roles, OIDC federation, and short-lived tokens over static cloud keys.
- If a secret must pass through Terraform, minimize the number of humans and systems that can read state and plans.
- Rotation that restarts every service simultaneously is an availability incident disguised as hygiene.
Was this lesson helpful?