Prompt
How do I handle secrets safely in Terraform and Pulumi?
Latest observation
Handling secrets safely in Terraform and Pulumi means protecting them at rest, in transit, and in your workflows. The big rule is: don’t rely on either tool as a secret vault. Use them to reference secrets, not to store them as plain text.
General best practices
- Use a dedicated secret manager:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
- Kubernetes secrets only for low-trust/internal use, ideally with encryption at rest
- Never hardcode secrets in
.tf,.tfvars, Pulumi code, or config checked into Git. - Avoid printing secrets in logs,
output, debug traces, CI logs, or shell history. - Limit access with least privilege and short-lived credentials.
- Rotate secrets regularly and have an incident plan.
- Use separate environments/workspaces/stacks so a secret used in one env doesn’t leak into others.
Terraform: safe secret handling
1) Mark variables and outputs as sensitive
Terraform can hide values from CLI output, but this is not encryption.
variable "db_password" {
type = string
sensitive = true
}
output "db_password" {
value = var.db_password
sensitive = true
}
Important
sensitive = trueonly hides display in CLI/UI.- The value may still exist in state.
2) Store state securely
Terraform state often contains secrets, even if marked sensitive.
- Use a remote backend with encryption:
- S3 + SSE-KMS
- Azure Storage encryption
- GCS with CMEK if needed
- Terraform Cloud/Enterprise
- Restrict access to the state backend tightly.
- Enable versioning and audit logs where possible.
- Never commit
terraform.tfstateto Git.
3) Prefer pulling secrets from a secret manager
Examples:
data "aws_secretsmanager_secret_version" ...data "azurerm_key_vault_secret" ...data "vault_kv_secret_v2" ...
Then pass the secret only where needed.
4) Avoid secret values in resource arguments if possible
Some resources will store the secret in state no matter what. If the provider requires it, assume it may end up in state.
If possible:
- Use references to secrets rather than inline values
- Use ephemeral bootstrap credentials, then let the app fetch secrets at runtime
5) Be careful with provisioners and templates
Provisioners can leak secrets via:
- shell command history
- logs
user_data- rendered templates
Avoid things like:
provisioner "local-exec" {
command = "echo ${var.password}"
}
Pulumi: safe secret handling
Pulumi has stronger built-in secret support than Terraform, but you still need to use it correctly.
1) Mark secrets as secrets
Use Pulumi secret APIs or config secrets.
In code
import * as pulumi from "@pulumi/pulumi";
const password = pulumi.secret("super-secret-password");
In config
pulumi config set --secret dbPassword "super-secret-password"
Pulumi encrypts secret config in the stack state.
2) Secret propagation
Pulumi tracks secrets through outputs and transformations. If a secret is used in an output, the output stays secret.
const connString = pulumi.interpolate`postgres://user:${password}@host/db`;
This stays secret if password is secret.
3) Use a proper secrets provider
Pulumi supports secrets providers such as:
- Pulumi service-managed encryption
- AWS KMS
- Azure Key Vault
- GCP KMS
- HashiCorp Vault
Set this when creating the stack or backend, so secrets are encrypted with your chosen key management.
4) Don’t unwrap secrets unless absolutely necessary
Avoid:
- logging secret values
- converting to plain strings too early
- passing secrets into code that doesn’t understand Pulumi secrets
Use apply carefully:
password.apply(p => {
// avoid console.log(p)
return someResource(p);
});
5) State still matters
Even though Pulumi encrypts secret values in state, you still must:
- secure the backend
- restrict stack access
- avoid exporting secrets unnecessarily
Terraform vs Pulumi: practical difference
Terraform
- Has
sensitivemetadata, but state often still contains the secret - Best practice is mostly about minimizing exposure and securing state
- Secret handling is less native; relies heavily on external secret managers
Pulumi
- Has built-in encrypted secret handling
- Secrets propagate through outputs more naturally
- Still requires secure state/backends and disciplined logging
Recommended patterns
Pattern 1: App fetches secrets at runtime
Best overall.
- Terraform/Pulumi deploy infrastructure
- App uses IAM/workload identity to read secrets from a secret manager at runtime
This avoids putting secrets into IaC state whenever possible.
Pattern 2: IaC reads secret manager, passes secret to resource
Use only when necessary.
- Terraform/Pulumi fetch secret from manager
- Inject into resource configuration
- Secure backend and restrict access tightly
Pattern 3: Use ephemeral/generated secrets
Generate a password, store it in a secret manager, and rotate it.
- Avoid manual handling
- Automate rotation
- Ensure generated password doesn’t get printed
CI/CD tips
- Mask secrets in CI logs
- Use OIDC/workload identity instead of long-lived access keys
- Never echo config or environment variables containing secrets
- Store Terraform/Pulumi backend credentials in the CI secret store
- Use separate identities for plan vs apply if possible
Common mistakes to avoid
- Committing
.tfvars,.env, or Pulumi config files with secrets - Treating
sensitiveas encryption - Logging
terraform outputwithout filtering - Using
local-execwith secret-bearing commands - Storing secrets in plain text in GitHub Actions or CI logs
- Assuming deleted resources remove secrets from state history
Quick rule of thumb
- Terraform: secrets can be hidden from output, but state may still contain them, so secure state and prefer external secret stores.
- Pulumi: use
pulumi.secret()and secret config, but still secure backend/state and avoid unwrapping secrets.
If you want, I can also give you:
- a Terraform example with AWS Secrets Manager,
- a Pulumi example with encrypted config, or
- a checklist for CI/CD secret hygiene.