Opscraft
Terraform · Lesson 9 of 12 · 3 min read

Secrets and security in Terraform

  • Never hardcode secrets in .tf or 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 diagramLeaksSaferHardcoded in .tf or committed .tfvarsState file (even with sensitive = true)Plan output in CI logsmanage_master_user_password = trueEphemeral resources, write-only argumentsState bucket treated as a secret store
Figure 1. Where secrets leak in Terraform, and the safer pattern: let AWS hold the secret so it never enters state.
Swipe sideways to see all of it, or tap to open it full screen.