Prompt
How do I keep dev, staging, and prod environment parity using infrastructure as code?
Latest observation
To keep dev, staging, and prod parity with infrastructure as code, aim for same architecture, same deployment process, different configuration.
Core principles
1) Use one infrastructure codebase
Define all environments in the same IaC repository or module set:
- Terraform / Pulumi / CloudFormation / Bicep, etc.
- Shared modules for networking, compute, databases, queues, observability
- Environment-specific wrappers or config files
This avoids “snowflake” environments.
2) Make environments differ only by inputs
Keep code identical, vary only:
- instance sizes / replica counts
- domain names
- feature flags
- secrets
- scaling thresholds
- retention periods
- nonfunctional limits
Example:
- dev: 1 small app node
- staging: same app, smaller scale than prod but same topology
- prod: same modules, higher availability and capacity
3) Mirror production topology where it matters
For parity, staging should resemble prod in:
- cloud provider / region pattern
- VPC/network segmentation
- load balancer type
- service mesh / ingress setup
- database engine/version
- cache/queue services
- IAM model
- logging/monitoring stack
You don’t need identical size, but avoid different patterns that hide bugs.
4) Promote the same artifact through environments
Build once, deploy many:
- same container image or package
- same runtime version
- same config template
- no rebuilding per environment
This reduces “works in staging, fails in prod” issues from drift.
5) Parameterize everything environment-specific
Use variables, not duplicated code:
- Terraform workspaces or separate state per env
- environment tfvars / YAML config / parameter files
- secret managers for sensitive values
Avoid copy-pasting full stacks for each environment.
6) Keep state isolated but structure shared
Each environment should have:
- separate state
- separate cloud accounts or projects if possible
- separate IAM boundaries
- separate secrets
- separate DNS and data stores
But all should come from the same modules and conventions.
7) Use automated validation
Parity is not only structural; enforce it with tests:
terraform planin CI for each env- policy-as-code checks
- integration tests against staging
- smoke tests after deploy
- drift detection against live infrastructure
8) Control drift aggressively
Prevent manual changes in console:
- restrict write access
- use IaC as the source of truth
- run scheduled drift detection
- alert on out-of-band changes
9) Version everything
Keep these aligned across envs:
- module versions
- provider versions
- OS/base images
- application runtime versions
- schema migrations
10) Design staging to be “prod-like,” not “cheap-like”
A common mistake is making staging structurally different to save cost. Better:
- keep architecture the same
- reduce scale
- use smaller SKUs
- disable nonessential traffic
- use synthetic or masked data
Practical pattern
A common layout:
infra/
modules/
vpc/
app/
db/
envs/
dev/
main.tf
dev.tfvars
staging/
main.tf
staging.tfvars
prod/
main.tf
prod.tfvars
All envs call the same modules; only the variables differ.
Good parity checklist
Use the same:
- cloud provider and region strategy
- network layout
- service types
- deployment pipeline
- artifact versioning
- observability stack
- IAM patterns
- backup/restore mechanisms
Allow differences only in:
- capacity
- secrets
- external endpoints
- data volume
- retention and alert thresholds
Common anti-patterns
- separate IaC codebases per environment
- manual prod hotfixes
- staging running a different database or queue technology
- rebuilding artifacts separately for each env
- using local mocks in dev that don’t resemble prod
- allowing untracked console changes
Rule of thumb
If a change requires editing one environment’s infrastructure code differently from the others, ask:
“Is this truly an environment-specific parameter, or are we introducing drift?”
If it’s not essential, keep it shared.
If you want, I can also give you:
- a Terraform example for dev/staging/prod parity, or
- a CI/CD workflow that promotes the same artifact across environments.