Prompt
How do I fix secrets handling in infrastructure code?
Latest observation
Secrets handling in infrastructure code usually goes wrong in a few predictable ways: secrets get committed to Git, passed through plain-text variables, logged during deploys, or copied into multiple places manually. The fix is to centralize secret storage, inject secrets at runtime, and make sure your IaC only references secret locations—not the secret values themselves.
Good rules of thumb
-
Never hardcode secrets in IaC
- Don’t put passwords, API keys, private keys, tokens, or connection strings directly in Terraform/CloudFormation/Pulumi/CDK templates.
-
Store secrets in a dedicated secret manager
- Use something like:
- AWS Secrets Manager or SSM Parameter Store
- Azure Key Vault
- GCP Secret Manager
- HashiCorp Vault
- Keep secrets encrypted at rest and access-controlled.
- Use something like:
-
Pass references, not values
- Your infrastructure code should point to:
- secret names
- secret ARNs/IDs
- secret version references
- The application or runtime environment retrieves the secret when needed.
- Your infrastructure code should point to:
-
Use runtime injection
- Inject secrets into containers, VMs, serverless functions, or CI/CD jobs at deploy/runtime via:
- environment variables from secret stores
- mounted secret files
- cloud-native secret integrations
- Avoid baking secrets into images or immutable artifacts.
- Inject secrets into containers, VMs, serverless functions, or CI/CD jobs at deploy/runtime via:
-
Limit who/what can access secrets
- Use least privilege IAM/RBAC.
- Separate read access for deploy systems vs application runtime.
- Restrict developers from broad secret access unless required.
-
Prevent secret leakage in logs and state
- Mark outputs as sensitive.
- Don’t print env vars or command lines containing secrets.
- Be careful with Terraform state: it can contain secret values.
- Encrypt state and lock down state storage access.
-
Rotate secrets regularly
- Use short-lived credentials where possible.
- Automate rotation and update consumers safely.
-
Scan for secret exposure
- Use pre-commit hooks, secret scanning in CI, and repository scanning tools.
- Add policy checks to fail builds if secrets are found.
If you’re using Terraform
Terraform is a common place where secrets leak.
Avoid
variable "db_password" {
default = "supersecret"
}
Better
variable "db_password" {
sensitive = true
}
But note: sensitive = true only hides it in CLI output; it does not prevent it from being stored in state.
Better pattern
- Read secret values from a secret manager data source
- Or pass them at apply time from a secure pipeline secret store
- Or have the app fetch them at runtime instead of Terraform managing the value directly
Example:
data "aws_secretsmanager_secret_version" "db" {
secret_id = "prod/db"
}
locals {
db_password = jsondecode(data.aws_secretsmanager_secret_version.db.secret_string)["password"]
}
Be careful: this still puts the secret into Terraform state if Terraform reads it.
If possible, use Terraform to:
- create the secret container/resource
- grant permissions
- configure consumers to fetch it directly
- avoid ever reading the secret value into Terraform when you can
Best practice architecture
A solid pattern looks like this:
- IaC creates the secret resource
- e.g.
aws_secretsmanager_secret
- e.g.
- CI/CD writes the secret value
- from a protected pipeline variable or external secret source
- App runtime reads the secret
- using IAM role / workload identity
- Terraform never stores the secret plaintext in state
- only the secret reference/metadata
Common mistakes to avoid
- Committing
.envfiles to Git - Putting secrets in user data scripts
- Using default provider debug logs in CI
- Storing secret values in Terraform outputs
- Passing secrets through many layers of modules
- Reusing the same long-lived credential everywhere
- Keeping secrets in plain text in cloud-init, Helm values, or Kubernetes manifests
If this is Kubernetes
Use:
- External Secrets Operator
- Sealed Secrets
- CSI secret store drivers
- Native cloud secret integrations
Avoid:
- plaintext
Secretmanifests in Git unless encrypted or sealed - embedding secrets into container images
- mounting secrets via ConfigMaps
A practical checklist
- Remove hardcoded secrets from code and templates
- Move secrets to a secret manager
- Replace plaintext vars with secret references
- Ensure IaC state is encrypted and access-controlled
- Mark sensitive outputs as sensitive
- Disable/avoid debug logging that prints secrets
- Add secret scanning in CI and Git hooks
- Rotate any leaked credentials
- Use short-lived auth where possible
If you want, I can give you a tool-specific fix for Terraform, Pulumi, CloudFormation, Kubernetes, or GitHub Actions.