INF-11Recommended
Build every environment from the same code
Build all environments from the same modules. Let them differ only in values such as sizes, counts and names, kept in one file per environment.
How to do it
Keep one module per layer and one small variables file per environment. Never write if environment == "production" to create a different kind of resource. When production needs a feature, such as a database replica, give staging the same feature at a smaller size. Run the same database engine and versions everywhere.
Examples
Terraform example
# production.tfvars
environment = "production"
instance_size = "m7g.large"
instance_count = 3
database_size = "db.r7g.large"
# staging.tfvars: same resources, smaller
environment = "staging"
instance_size = "t4g.small"
instance_count = 1
database_size = "db.t4g.medium"
How to check
A diff between the staging and production folders shows only variables and backend settings. Both environments report the same module versions and engine versions.
Sources
Related rules
- TST-26 Keep a staging environment with generated data in Testing
- STK-108 Use Cloud Run or AWS Fargate when one host is not enough in Software choices
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/environments/same-code-every-environment">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.