Prompt
How do I handle secrets safely in infrastructure as code?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
Safely handling secrets in infrastructure as code (IaC) is mostly about never hardcoding secrets into your code, state, or logs and using a dedicated secrets system with tight access controls.
Core principles
-
Don’t commit secrets to Git
- Avoid putting passwords, API keys, private keys, tokens, or connection strings directly in Terraform/CloudFormation/Pulumi code.
- Assume anything in source control can be copied, mirrored, or leaked.
-
Use a secrets manager
- Store secrets in tools like:
- AWS Secrets Manager / SSM Parameter Store
- Azure Key Vault
- Google Secret Manager
- HashiCorp Vault
- Kubernetes Secrets only with encryption at rest and access controls
- IaC should reference secrets by name/ID, not contain the secret value itself.
- Store secrets in tools like:
-
Minimize secret exposure in state
- Many IaC tools store resource attributes in state, and state can contain secrets.
- Protect state carefully:
- encrypt at rest
- restrict access
- use remote backends with strong IAM/RBAC
- avoid sharing state broadly
- Know whether your tool marks outputs as sensitive, and remember “sensitive” often only hides display, not storage.
-
Use environment variables or runtime injection for ephemeral values
- For CI/CD or local runs, inject secrets at runtime from a vault or CI secret store.
- Prefer short-lived credentials over long-lived static ones.
-
Separate configuration from secret material
- Keep non-sensitive config in IaC.
- Load secrets from secure references at deploy time or app startup.
-
Rotate secrets regularly
- Use automated rotation where possible.
- Design apps and IaC so rotation doesn’t require manual changes everywhere.
-
Limit blast radius
- Use least privilege for both humans and automation.
- Separate secrets per environment, application, and service.
-
Scan for secret leaks
- Add secret scanning to:
- pre-commit hooks
- CI pipelines
- repository scanners
- Examples: Gitleaks, TruffleHog, GitHub secret scanning.
- Add secret scanning to:
Tool-specific guidance
Terraform
- Mark variables/outputs as
sensitive = true, but treat this as display protection only. - Avoid putting secret values in
tfvarsfiles committed to Git. - Use data sources to read secrets from a secrets manager.
- Secure the Terraform state backend aggressively.
CloudFormation / ARM / Pulumi
- Use references to secret stores or dynamic references instead of literal values.
- Use service-managed identity to retrieve secrets at runtime instead of embedding them.
Kubernetes
- Don’t rely on plain
Secretobjects alone unless you’ve enabled encryption at rest and enforced RBAC. - Prefer external secret operators/integrations that sync from a real secret manager.
CI/CD best practices
- Store secrets in the CI platform’s secret store, not in pipeline YAML.
- Use OIDC/workload identity to get temporary cloud credentials instead of static access keys.
- Mask secrets in logs and avoid echoing variables.
A safe pattern
Instead of:
variable "db_password" {
default = "supersecret123"
}
Do:
data "aws_secretsmanager_secret_version" "db" {
secret_id = "prod/db"
}
Then have your app or module read the secret at deploy/runtime, rather than embedding the secret directly in IaC.
Practical checklist
- No secrets in Git
- No secrets in IaC variables or outputs unless absolutely necessary
- Encrypted, access-controlled state
- Dedicated secrets manager
- Short-lived credentials where possible
- Secret scanning in CI/CD
- Rotation plan
- Least privilege for humans and automation
If you want, I can give you a Terraform-specific example, a Kubernetes example, or a tool-agnostic reference architecture for handling secrets.