Infrastructure as code
Create every resource from code in version control. Keep state locked and encrypted, review each plan, apply only from CI and catch drift daily.
- Required 3
- Recommended 6
Infrastructure as code means a file describes each server, database, network and DNS record. A tool compares the file with what exists and makes the difference. The examples use OpenTofu and Terraform, which share one language. Pulumi, AWS CloudFormation, Azure Bicep and Google's Config Connector follow the same rules.
Required
Keep state remote with locking and encryption
INF-2Keep the state file in a remote backend that locks, encrypts and keeps old versions. Only the deploy roles may read it. Never keep state in git or on a laptop.
Review the plan and apply only from CI
INF-3Post the plan in every pull request. Apply only from CI after merge, from the plan file made for that commit. No person applies from a laptop.
Protect resources that hold data
INF-8Turn on deletion protection for every database, bucket and disk that holds data. Stop any plan that deletes or replaces one until a second person approves.
Recommended
Define every resource in code
INF-1Create and change every cloud resource, firewall rule and DNS record through code in version control. Never make a lasting change in a web console.
Pin the tool and provider versions
INF-4Pin the version of the tool and of every provider, and commit the provider lock file with hashes for every platform you run on.
Split state by environment and layer
INF-5Give each environment and each layer its own state, such as network, data and app. One mistake then touches only one of them.
Reuse versioned modules
INF-6Put each repeated pattern in a module with checked inputs, and pin every use of it to a version. Never copy the same resources between environments.
Detect drift every day
INF-7Run a plan against every state at least once a day. Open a ticket when it finds a change nobody made in code.
Bring hand-made resources under code
INF-9Import every resource that was made by hand into code before you change it. Then tag everything the code manages, so the rest stands out.
Read this with
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">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.