Prompt
How do I integrate secrets management with Terraform?
Latest observation
The safest way to integrate secrets management with Terraform is to avoid putting secrets directly in Terraform code or state whenever possible.
Core principles
-
Don’t hardcode secrets
- Never put passwords, API keys, private keys, or tokens directly in
.tffiles. - Avoid passing secrets as plain-text variables in CI logs or shell history.
- Never put passwords, API keys, private keys, or tokens directly in
-
Keep secrets out of Terraform state
- Terraform state can contain sensitive values even if marked
sensitive = true. - Sensitive values are hidden in CLI output, but still stored in state.
- Terraform state can contain sensitive values even if marked
-
Use Terraform to reference secrets, not own them
- Let a dedicated secrets system store and rotate secrets.
- Terraform should usually provision the secret container/policy/access, not the secret value itself.
Common patterns
1) Read secrets from a secrets manager at runtime
Use a secrets service such as:
- HashiCorp Vault
- AWS Secrets Manager / SSM Parameter Store
- Azure Key Vault
- Google Secret Manager
Example: AWS Secrets Manager via data source
data "aws_secretsmanager_secret_version" "db" {
secret_id = "prod/db"
}
locals {
db_secret = jsondecode(data.aws_secretsmanager_secret_version.db.secret_string)
}
Then use values carefully:
resource "aws_db_instance" "main" {
username = local.db_secret.username
password = local.db_secret.password
}
Caution: this may still place secret values in Terraform state. Use only when necessary.
2) Store secret values outside Terraform, pass only references
A better pattern is:
- Terraform creates the secret resource/container
- Another process populates the secret value
- Applications fetch the secret directly at runtime
Example:
resource "aws_secretsmanager_secret" "db" {
name = "prod/db"
}
Then inject the secret from your deployment pipeline or a separate secret-rotation process.
3) Use environment variables for provider auth, not application secrets
For example:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYARM_CLIENT_SECRETGOOGLE_APPLICATION_CREDENTIALS
These are for Terraform/provider authentication, not for application secrets.
4) Use sensitive variables, but understand the limits
variable "db_password" {
type = string
sensitive = true
}
This hides the value in plan/apply output, but:
- it does not encrypt state by itself
- the secret may still be stored in state if used in resources
Recommended workflow
Best practice
- Keep secret values in a dedicated secrets manager
- Give Terraform permission to create/update secret resources or access policies
- Use Terraform to configure applications to read secrets at runtime
- Use remote state with encryption and restricted access
If Terraform must manage the secret value
- Mark variable outputs as
sensitive = true - Use encrypted remote state
- Restrict access to state files
- Minimize the number of resources that consume the secret
- Rotate secrets frequently
Protecting state
Because Terraform state is often the weak point, use:
- Encrypted backend (e.g. S3 with SSE-KMS, Azure Storage encryption, GCS encryption)
- Access controls on the backend
- State locking
- Separate workspaces/accounts/projects for isolation
CI/CD considerations
- Never echo secrets in pipeline logs
- Inject secrets via secure environment variables or secret stores in your CI system
- Redact outputs
- Use short-lived credentials when possible
- Prefer OIDC-based auth to static cloud keys
Rule of thumb
If a secret is needed by:
- Terraform itself → use a secret manager or CI secret injection
- The resource being created → prefer a separate secret manager and runtime retrieval
- An application → store it in a secret manager, not in Terraform state
If you want, I can show you a provider-specific example for AWS, Azure, GCP, or Vault.