Prompt
How do I standardize infrastructure across dev, staging, and prod with IaC?
Latest observation
A good way to standardize infrastructure across dev, staging, and prod with IaC is to keep the infrastructure shape the same and parameterize only the things that should differ.
Core approach
1) Use one reusable infrastructure definition
Define your app stack once in Terraform, Pulumi, CloudFormation, Bicep, etc., and reuse it for each environment.
- Same VPC/network layout
- Same service types
- Same IAM model
- Same deployment process
Then vary only environment-specific values like:
- instance sizes
- replica counts
- domain names
- feature flags
- secrets
- scaling limits
2) Separate environment configuration from infrastructure code
A common pattern:
- modules/: reusable building blocks
- envs/dev
- envs/staging
- envs/prod
Example structure:
infra/
modules/
app/
network/
database/
envs/
dev/
staging/
prod/
Each environment folder supplies:
- variables
- backend config/state
- environment-specific tags
- sizing and policy settings
3) Keep environments as similar as possible
Standardization works best when dev, staging, and prod differ only in scale, not architecture.
Good:
- same load balancer type
- same database engine
- same container orchestration platform
- same monitoring and logging stack
Avoid:
- dev on serverless, prod on VMs
- staging using a different database engine
- manual edits in prod only
4) Use modules for consistency
Encapsulate patterns into modules so every environment uses the same implementation.
For example, a database module can enforce:
- encryption at rest
- backups enabled
- private networking
- standardized tags
- logging settings
This reduces drift and makes review easier.
5) Parameterize by environment, not by ad hoc edits
Use variables like:
environment = dev|staging|prodreplicas = 1, 2, 4instance_class = t3.medium | m6i.largeenable_autoscaling = true/false
Avoid copying and editing files manually for each environment.
6) Use separate state per environment
Never share a single state file across all environments.
Benefits:
- isolated failures
- safe applies
- cleaner rollback
- no cross-environment impact
Typical options:
- separate Terraform workspaces
- separate state files/buckets
- separate stacks/projects per environment
A common best practice is one state per environment.
7) Put guardrails in place
Standardization should include policy enforcement:
- required tags
- encryption
- least-privilege IAM
- approved instance types
- network restrictions
- CI checks before apply
Tools:
- Sentinel / OPA / Conftest
- Terraform validation
- policy-as-code in CI
8) Automate promotions through environments
Treat dev → staging → prod as a promotion pipeline:
- deploy to dev
- run tests
- promote same code/config baseline to staging
- validate
- promote to prod
This helps ensure prod is not “special.”
9) Manage secrets and config separately
Do not hardcode secrets in IaC.
Use:
- secret managers
- environment-specific secret stores
- CI/CD variables
- encrypted values
Keep non-secret config in versioned IaC, and secret values outside it.
10) Document intended differences
Write down which values are allowed to differ between environments, for example:
- dev: small instances, no HA
- staging: prod-like topology, smaller scale
- prod: full HA, backups, stricter alerts
This prevents accidental drift.
Recommended pattern
A strong default is:
- Same modules
- Separate environment folders
- One state per environment
- Environment-specific variable files
- CI/CD for plan/apply
- Policy checks
- Immutable, versioned changes
Example with Terraform
infra/
modules/
webapp/
db/
envs/
dev/
main.tf
dev.tfvars
staging/
main.tf
staging.tfvars
prod/
main.tf
prod.tfvars
Each environment calls the same modules:
module "webapp" {
source = "../../modules/webapp"
environment = var.environment
instance_type = var.instance_type
replicas = var.replicas
}
Then dev.tfvars, staging.tfvars, and prod.tfvars control differences.
Common mistakes to avoid
- Copy-pasting entire stacks per environment
- Sharing state across environments
- Manual changes in prod
- Letting staging drift from prod
- Using different services in different environments
- Not enforcing policy
- Storing secrets in plaintext in IaC
If you want, I can show:
- a Terraform repo structure for dev/staging/prod,
- a Pulumi example, or
- a CI/CD pipeline pattern for promoting IaC changes.