Prompt
how to manage secrets in infrastructure as code
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
Managing secrets in Infrastructure as Code (IaC) is mostly about keeping sensitive values out of source code, state files, logs, and shared workflows.
Core principles
-
Never hardcode secrets in IaC files
- Don’t put passwords, API keys, tokens, or private keys directly in Terraform, CloudFormation, Pulumi, etc.
-
Use a dedicated secret store
- Common options:
- AWS Secrets Manager / SSM Parameter Store
- Azure Key Vault
- Google Secret Manager
- HashiCorp Vault
- Kubernetes Secrets (with extra protections)
- IaC should reference the secret location, not the secret value when possible.
- Common options:
-
Inject secrets at runtime
- Prefer environment variables, workload identity, or secret mounts over embedding values in templates.
-
Limit exposure in state
- Many IaC tools store values in state files.
- Treat state as sensitive:
- Encrypt it
- Restrict access
- Store it remotely with access controls
- Avoid outputting secrets from modules
Tool-specific guidance
Terraform
- Use
sensitive = truefor variables and outputs. - Prefer data sources that read from secret managers.
- Be careful: sensitive values can still end up in state.
- Use remote state with encryption and strict IAM.
- Example:
variable "db_password" { type = string sensitive = true } - Better pattern: read from secret manager:
data "aws_secretsmanager_secret_version" "db" { secret_id = "prod/db" } locals { db_password = jsondecode(data.aws_secretsmanager_secret_version.db.secret_string)["password"] }
Kubernetes / Helm
- Avoid plain
Secretmanifests in Git unless encrypted. - Use:
- External Secrets Operator
- Sealed Secrets
- SOPS with age/GPG
- Mount secrets as files or env vars.
CloudFormation / ARM / Bicep / Pulumi
- Use parameter/secret references from cloud secret stores.
- Mark inputs as secret/sensitive where supported.
- Avoid printing secret values in outputs.
Good workflows
- Store secrets in a secret manager.
- Commit only references or encrypted secret files.
- Use CI/CD to fetch secrets at deploy time with short-lived credentials.
- Rotate secrets regularly.
- Audit access and usage.
- Separate dev/staging/prod secrets.
What to avoid
- Committing secrets to Git
- Putting secrets in
.tfvarsor environment files without protection - Exposing secrets in logs or plan output
- Sharing state files casually
- Using long-lived static credentials if workload identity is available
Practical recommendations
- Best default: secret manager + IaC references + runtime injection
- For Git: encrypt secrets with SOPS or use External Secrets
- For Terraform: protect state and mark variables/output as sensitive
- For Kubernetes: prefer External Secrets or sealed/encrypted secrets over plaintext manifests
If you want, I can also give you:
- a Terraform example using AWS Secrets Manager,
- a Kubernetes + SOPS pattern, or
- a secret management checklist for CI/CD.