Prompt
How do I manage multiple cloud environments without copy-pasting configs?
Latest observation
A good way to avoid copy-pasting is to separate shared infrastructure definitions from environment-specific values and then generate/apply the right config per environment.
Here are the most common patterns, from simplest to most scalable:
1) Use a single base config with per-environment overrides
Keep one “base” config and override only what changes.
Example structure
infra/
base/
main.tf
variables.tf
envs/
dev.tfvars
staging.tfvars
prod.tfvars
Then apply:
terraform apply -var-file=envs/dev.tfvars
terraform apply -var-file=envs/prod.tfvars
This works well when only things like instance size, region, or replica count differ.
2) Use modules for reusable components
Put repeated infrastructure into reusable modules.
Example
modules/networkmodules/appmodules/database
Then each environment just calls the same modules with different inputs.
This gives you:
- less duplication
- consistent architecture
- easier updates
3) Use templates or parameterized manifests
If you’re working with YAML/JSON/Kubernetes manifests, use:
- Helm for Kubernetes
- Kustomize overlays
- Jsonnet / CUE
- Jinja2 templates in some pipelines
Example with Kustomize
base/
overlays/dev/
overlays/prod/
Shared resources live in base, and environment-specific patches live in overlays.
4) Store environment-specific values separately
Instead of duplicating full configs, keep only values in:
- environment variables
.tfvars- config files in S3/SSM/Secrets Manager/Vault
- CI/CD variables
This is especially useful for:
- account IDs
- regions
- CIDRs
- sizing
- feature flags
- secrets
5) Use workspaces carefully
Terraform workspaces can help separate states for dev/staging/prod, but they’re not a full replacement for env-specific config structure.
Good for:
- same code
- separate state
Less good for:
- large differences between environments
- strict isolation requirements
For many teams, separate directories or separate state backends per environment are clearer.
6) Use a CI/CD pipeline to select environment automatically
Have one pipeline that:
- detects branch/tag/environment
- picks the right config file
- deploys only that environment
Example:
main→ proddevelop→ staging- feature branches → dev/sandbox
7) Adopt GitOps if you’re on Kubernetes or declarative infra
Keep desired state in Git, and use controllers/tools like:
- Argo CD
- Flux
- Atlantis for Terraform workflows
This reduces manual config drift and makes environment promotion easier.
A practical recommendation
If you want the simplest robust setup:
- Use modules for reusable resources
- Use separate env files for values
- Use separate state per environment
- Deploy through CI/CD
That gives you:
- minimal duplication
- clear separation
- safer promotions from dev → staging → prod
Example Terraform pattern
live/
dev/
main.tf
terraform.tfvars
staging/
main.tf
terraform.tfvars
prod/
main.tf
terraform.tfvars
modules/
app/
network/
Each main.tf is small and mostly just wires modules together.
If you tell me which cloud/stack you’re using, I can suggest the best approach:
- AWS + Terraform
- Kubernetes + Helm/Kustomize
- Azure/Bicep
- GCP + Terraform
- multi-cloud setup