INF-5Recommended
Split state by environment and layer
Give each environment and each layer its own state, such as network, data and app. One mistake then touches only one of them.
How to do it
Use one folder or stack per environment and layer, each with its own state key. Put slow-changing layers, such as the network and the databases, apart from the app layer that changes often. Pass values between layers through outputs or a parameter store, never by copying IDs. Keep a plan under 2 minutes. When it runs longer, split the state again. Keep each environment in its own account too, as Give each environment its own account says.
Examples
Plain text example
infra/
modules/ shared building blocks
staging/network/ key: staging/network.tfstate
staging/app/ key: staging/app.tfstate
production/network/ key: production/network.tfstate
production/app/ key: production/app.tfstate
How to check
Every backend key names exactly one environment. state list in any app state shows no database or network resources. Each plan finishes in under 2 minutes.
Sources
Related rules
- INF-10 Give each environment its own account in Infrastructure and environments
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/infrastructure-as-code/split-state">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.