Terraform · Lesson 9 of 12 · 3 min read
Secrets and security in Terraform
- Never hardcode secrets in
.tfor committed.tfvars. - Mark variables and outputs
sensitive = true— this only hides them in CLI output; values still land in state. - Read secrets at apply time from Secrets Manager / SSM Parameter Store / Vault via data sources — but note those values are then stored in state too.
- Better: let AWS manage secrets so Terraform never sees them — e.g. RDS
manage_master_user_password = true(password lives in Secrets Manager), generate credentials outside Terraform, or use ephemeral resources (Terraform 1.10+) and write-only arguments (1.11+) designed to keep secrets out of state and plan files. - Protect the state bucket as a secret store (KMS, restricted IAM, logging).
- Scan for secrets in repos (gitleaks) and for misconfigurations in code (Trivy/Checkov) in CI.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.