Prompt
How do I prevent configuration drift across dev, staging, and prod?
Latest observation
Preventing configuration drift across dev, staging, and prod comes down to making environments as repeatable, declarative, and automated as possible.
Core principles
1. Define config as code
Store all environment-specific configuration in version control:
- app settings
- infrastructure definitions
- deployment manifests
- secrets references
- feature flags
Use tools like:
- Terraform / Pulumi / CloudFormation for infra
- Helm / Kustomize / plain Kubernetes manifests for deployments
- Ansible / Chef / Puppet for OS-level config where needed
2. Use a single baseline
Keep a shared “base” config and only override what truly differs:
basedevstagingprod
This reduces copy-paste drift. Prefer layered configs rather than separate fully duplicated files.
3. Automate environment creation
Never hand-build environments. Use CI/CD or provisioning pipelines so each environment is created the same way every time.
4. Standardize secrets management
Don’t hardcode secrets in env files or scripts. Use a central secrets manager:
- AWS Secrets Manager / SSM Parameter Store
- Azure Key Vault
- GCP Secret Manager
- Vault
Keep only secret references in code, not the secret values themselves.
5. Make prod and staging as similar as possible
Differences should be intentional and minimal:
- scale/capacity
- external endpoints
- credentials
- feature flags
- logging levels
Avoid changing libraries, runtime versions, or OS packages between environments unless required.
6. Detect drift continuously
Regularly compare actual state vs desired state:
- Terraform plan in CI
- Kubernetes drift detection
- configuration audits
- policy-as-code checks
Alert when manual changes are made outside the pipeline.
7. Restrict manual changes
Limit direct console/SSH edits in staging and prod.
- Use change management
- enforce least privilege
- require all changes through PRs and pipelines
8. Use immutable artifacts
Build once, promote the same artifact through dev → staging → prod. Do not rebuild differently for each environment. Only inject environment-specific config at deploy time.
9. Add validation and tests
Before deployment, validate config:
- schema validation
- policy checks
- smoke tests
- integration tests
- config linting
10. Track environment differences explicitly
Document intentional differences in one place:
- DNS names
- instance sizes
- autoscaling thresholds
- third-party integrations
- feature flags
This helps distinguish “expected variation” from “drift.”
Practical workflow
A good pattern is:
- Developer commits config changes to Git
- CI validates and tests them
- Infrastructure/deployment pipeline applies them
- Environment is continuously reconciled to the declared state
- Drift detection alerts on out-of-band changes
Common mistakes to avoid
- Maintaining separate handwritten config files per environment
- Editing prod manually “just this once”
- Rebuilding app artifacts differently per environment
- Storing secrets in Git
- Letting staging and prod diverge over time
If you want, I can also give you:
- a Terraform-based example
- a Kubernetes/Helm setup
- or a checklist for preventing drift in a small team