Prompt
How do I separate dev, staging, and prod infrastructure cleanly?
Latest observation
A clean separation usually comes down to three layers of isolation:
- Configuration
- State / deployment boundaries
- Access and blast-radius controls
Here’s a practical way to do it.
1) Use separate environments, not just flags
Treat dev, staging, and prod as distinct environments with their own:
- credentials
- resource names
- databases
- queues/topics
- caches
- secrets
- DNS/domains
- monitoring alerts
Avoid a single shared infrastructure setup with a ENV=prod|staging|dev switch unless the environment is tiny. That tends to leak risk.
2) Separate by account/project/subscription if possible
Best practice is strong isolation at the cloud boundary:
- AWS: separate accounts
- GCP: separate projects
- Azure: separate subscriptions or resource groups, ideally subscriptions for prod vs non-prod
Typical pattern:
devaccount/projectstagingaccount/projectprodaccount/project
This gives you better:
- IAM separation
- billing separation
- policy enforcement
- reduced accidental impact
3) Use Infrastructure as Code with environment overlays
Keep one reusable base, but instantiate it per environment.
Examples:
- Terraform modules +
dev.tfvars,staging.tfvars,prod.tfvars - Pulumi stacks
- CloudFormation/SAM per stack
- Kubernetes overlays via Kustomize or Helm values files
Structure:
- shared module code
- environment-specific inputs
- environment-specific state
Example Terraform layout:
infra/
modules/
app/
network/
envs/
dev/
main.tf
dev.tfvars
staging/
main.tf
staging.tfvars
prod/
main.tf
prod.tfvars
4) Keep separate state per environment
Do not share the same Terraform state file across envs.
Use:
- different backend keys/buckets
- or separate state workspaces only if you understand the tradeoffs
Prefer:
state/dev/...state/staging/...state/prod/...
This prevents one environment’s changes from colliding with another’s.
5) Use separate secrets and config
Never reuse prod secrets in non-prod.
Store:
- API keys
- DB passwords
- OAuth client secrets
- signing keys
in environment-specific secret managers:
- AWS Secrets Manager / SSM Parameter Store
- GCP Secret Manager
- Azure Key Vault
- Vault
Also keep config values separated:
- URLs
- feature flags
- email/SMS providers
- third-party integrations
In staging, use sandbox integrations where possible.
6) Make prod harder to touch
Add guardrails:
- stricter IAM on prod
- approvals for prod deployments
- require tagged changes / protected branches
- restrict manual console access
- MFA for prod access
- separate CI/CD role for prod
- policy-as-code rules to block risky changes
Good pattern:
- dev: engineers can experiment freely
- staging: production-like, but limited blast radius
- prod: tightly controlled, audited, and approved
7) Make staging resemble prod
Staging should mirror prod as closely as practical:
- same instance types/classes when feasible
- same network topology
- same container images
- same autoscaling behavior
- same deployment pipeline
The goal is to catch integration issues before prod.
The main differences should be:
- smaller scale
- fake/sandboxed external dependencies
- less privileged access
8) Separate DNS, domains, and endpoints
Examples:
dev.api.example.comstaging.api.example.comapi.example.com
Likewise for frontends:
dev.example.comstaging.example.comwww.example.com
This avoids accidental cross-environment calls.
9) Isolate data
Never let dev or staging read/write prod data directly.
Instead:
- create synthetic test data
- anonymize data if copied from prod
- use one-way sanitized snapshots for staging if needed
- ensure backups and replicas are environment-specific
10) Use CI/CD promotion, not separate ad hoc deploys
A solid flow is:
- merge to dev branch → deploy to dev
- promote same build artifact to staging
- approve and promote same artifact to prod
Important:
- build once, deploy many
- don’t rebuild differently for prod
- use immutable artifacts with environment-specific config injected at deploy time
11) Enforce with naming and tagging conventions
Use consistent labels:
env=dev|staging|prodapp=myserviceowner=team-x
Resource names can encode env too:
myservice-dev-dbmyservice-staging-cachemyservice-prod-lb
Tags help with:
- cost allocation
- automation
- cleanup
- policies
12) Recommended minimal model
If you want a simple but strong setup:
- Separate cloud account/project for each env
- Separate IaC state per env
- Separate secrets per env
- Separate DNS/subdomains
- Staging mirrors prod
- CI/CD promotes one artifact through envs
- Strong IAM and approval gates for prod
Common mistakes to avoid
- Sharing a database between staging and prod
- Reusing the same secret across environments
- Using one Terraform state for everything
- Letting dev deploy directly to prod
- Running staging with drastically different architecture than prod
- Copying prod credentials into lower envs
- Allowing lower envs to access prod services
If you want, I can also give you:
- a Terraform example
- an AWS/GCP/Azure-specific setup
- or a Kubernetes namespace vs account/project isolation recommendation