Prompt
How do I separate secrets and environment config in Terraform?
Latest observation
A good rule of thumb is:
- Terraform state should not contain secrets unless you’re comfortable protecting state like a secret store.
- Environment config (region, instance sizes, feature flags, account IDs, etc.) is fine to pass as variables.
- Secrets should usually come from a secret manager or CI/CD environment, not hardcoded in
.tffiles.
Recommended patterns
1) Use input variables for non-secret environment config
Put config in variables.tf and supply values per environment.
variable "region" {
type = string
}
variable "instance_type" {
type = string
default = "t3.micro"
}
Then set values via:
terraform.tfvars*.auto.tfvars- CLI
-var - CI/CD pipeline variables
- workspace-specific variable files
Example prod.tfvars:
region = "us-east-1"
instance_type = "t3.small"
Run:
terraform apply -var-file=prod.tfvars
2) Treat secrets differently from config
Prefer pulling secrets from an external secret store:
- AWS Secrets Manager / SSM Parameter Store
- Azure Key Vault
- Google Secret Manager
- Vault
- CI/CD secret variables
Example using AWS Secrets Manager:
data "aws_secretsmanager_secret_version" "db" {
secret_id = "prod/db"
}
locals {
db_secret = jsondecode(data.aws_secretsmanager_secret_version.db.secret_string)
}
Then use local.db_secret.password where needed.
3) Mark sensitive variables
If you must pass a secret into Terraform, declare it sensitive:
variable "db_password" {
type = string
sensitive = true
}
This helps hide it in plan/apply output, but does not prevent it from being stored in state if used in a resource argument.
4) Keep secrets out of .tfvars when possible
Avoid committing secret values in:
terraform.tfvars*.tfvars- module defaults
- plain text
.tffiles
If you must use *.tfvars, keep them out of git and load them locally only.
5) Use separate files for separation of concerns
A common structure:
terraform/
main.tf
variables.tf
outputs.tf
env/
dev.tfvars
prod.tfvars
And secrets come from:
- environment variables
- secret manager data sources
- CI/CD secret injection
6) Be aware of state sensitivity
Even if a value is marked sensitive, Terraform state may still contain it. So:
- encrypt remote state
- restrict access to state backend
- use backend-specific encryption and IAM controls
- avoid outputting secrets
Example pattern
variable "environment" {
type = string
}
variable "region" {
type = string
}
variable "db_password" {
type = string
sensitive = true
}
prod.tfvars:
environment = "prod"
region = "us-east-1"
Secret supplied separately:
terraform apply \
-var-file=prod.tfvars \
-var='db_password=supersecret'
Better: pass it through CI secrets or a secret manager rather than the command line.
Practical guidance
- Config: tfvars, workspace vars, module inputs
- Secrets: secret manager, CI secret store, env vars, Vault
- Always: protect state, avoid committing secrets, mark secret variables sensitive
If you want, I can show you:
- a best-practice folder layout for dev/stage/prod, or
- an example using AWS Secrets Manager / SSM / Vault.