Prompt
How do I manage cloud infrastructure as code across multiple environments without copy-pasting?
Latest observation
A good way to avoid copy-pasting cloud IaC across environments is to make one reusable infrastructure definition and vary only the environment-specific inputs.
Core pattern
Use:
- Modules / components for reusable building blocks
- Separate environment configs for dev / staging / prod
- Shared defaults plus small overrides
- Remote state / dependency references where needed
- Promotion workflow so the same code moves through environments
Common structure
Example layout:
infra/
modules/
network/
app/
database/
envs/
dev/
main.tf
variables.tf
dev.tfvars
staging/
main.tf
staging.tfvars
prod/
main.tf
prod.tfvars
global/
provider.tf
versions.tf
What goes where
- modules/: reusable logic, no environment-specific values hardcoded
- envs/dev, envs/staging, envs/prod: small wrappers that call modules
- tfvars / yaml / json configs: env-specific values like instance size, scaling, CIDRs, feature flags
Key practices
1) Parameterize everything that changes
Instead of hardcoding:
- region
- instance types
- counts
- domain names
- tags
- CIDR ranges
- feature toggles
Pass them in as variables.
2) Keep modules generic
A module should not know whether it’s for dev or prod. It should just accept inputs and produce outputs.
3) Use composition, not duplication
Build small modules like:
- VPC/network
- compute
- DB
- monitoring
- IAM
Then compose them per environment.
4) Separate state per environment
Do not share the same state file between dev and prod.
Use:
- different backends/workspaces/accounts/projects
- different state names or buckets
- access controls per environment
5) Manage drift and changes through CI/CD
Use a pipeline that:
- validates formatting
- runs plan
- requires approval for prod
- applies only after review
6) Use overlays if your tool supports them
Depending on the tool:
- Terraform: modules + tfvars, sometimes Terragrunt
- Kubernetes manifests: Helm or Kustomize overlays
- Pulumi: config per stack
- CloudFormation: nested stacks / parameters
- Crossplane: compositions and claims
Example approach in Terraform
A simple env wrapper:
module "app" {
source = "../../modules/app"
name = var.name
region = var.region
instance_id = var.instance_id
replicas = var.replicas
}
And dev.tfvars:
name = "myapp-dev"
region = "us-east-1"
instance_id = "t3.small"
replicas = 1
prod.tfvars:
name = "myapp-prod"
region = "us-east-1"
instance_id = "m6i.large"
replicas = 3
Tips to keep it maintainable
- Use default values for most variables
- Use validation rules to prevent bad configs
- Keep environment differences small and explicit
- Avoid “if env == prod” logic scattered everywhere
- Prefer account/project separation for strong isolation
- Use tagging standards and naming conventions
Rule of thumb
If you find yourself copying 80% of the same code into another environment, you probably want:
- a module, or
- an overlay/config file, or
- a higher-level abstraction
If you want, I can show you a concrete example for Terraform, Pulumi, CloudFormation, or Kubernetes Helm/Kustomize.