Prompt
How do I manage multiple environments without copy-pasting IaC?
Latest observation
A good way to manage multiple environments without copy-pasting IaC is to keep one reusable infrastructure definition and vary only the environment-specific inputs.
Common patterns
1) Use variables/parameters
Define the resource logic once, and pass different values per environment.
- Terraform:
*.tfvarsfiles - CloudFormation: Parameters
- Pulumi: Config
- ARM/Bicep: parameter files
Example idea:
dev.tfvars→ small instance, fewer replicasprod.tfvars→ larger instance, autoscaling, stricter settings
2) Organize with modules/components
Put shared infrastructure into modules so each environment just instantiates them with different inputs.
Example structure:
infra/
modules/
network/
app/
db/
envs/
dev/
staging/
prod/
Each environment file is thin and only sets:
- size
- region
- scaling
- feature flags
- names/tags
- secrets references
3) Separate state per environment
Use separate state files/accounts/projects so environments don’t interfere.
Examples:
- different Terraform state files
- separate AWS accounts / Azure subscriptions / GCP projects
- separate namespaces in Kubernetes
This prevents accidental cross-environment changes.
4) Keep environment differences minimal
Try to make environments differ only in:
- capacity
- domain names
- feature toggles
- logging/monitoring levels
- credentials/secrets
- deletion protection / backups
If dev and prod diverge too much, you lose the benefit of IaC reuse.
5) Use overlays when needed
If you have a base config plus small environment-specific patches, use an overlay approach.
Common tools:
- Kustomize for Kubernetes
- Terragrunt for Terraform
- Helm values files for Kubernetes
- Jsonnet/CUE for generated config
Practical recommendation
If you’re starting fresh:
- Use modules for shared infrastructure
- Use one config per environment
- Use separate state per environment
- Pass differences through variables/config files
- Use CI/CD to apply the right environment automatically
Example Terraform layout
infra/
modules/
vpc/
app/
envs/
dev/
main.tf
dev.tfvars
prod/
main.tf
prod.tfvars
main.tf calls the modules once, and each env supplies different values.
If you want, I can show you a concrete example in Terraform, Kubernetes, or Bicep/CloudFormation.