INF-1Recommended
Define every resource in code
Create and change every cloud resource, firewall rule and DNS record through code in version control. Never make a lasting change in a web console.
How to do it
Pick one tool per project, such as OpenTofu, Terraform, Pulumi or CloudFormation. Give people read-only console access to staging and production, so a hand-made change cannot happen. Bring resources made by hand under code with import. A sandbox account for short experiments is the only exception, and it is wiped on a schedule. It applies Keep every change in version control to the cloud itself.
Examples
Terraform example
resource "aws_s3_bucket" "invoices" {
bucket = "example-shop-invoices"
}
TypeScript example
// Pulumi in TypeScript: the same bucket
import * as aws from '@pulumi/aws';
export const invoices = new aws.s3.Bucket('invoices', { bucket: 'example-shop-invoices' });
Python example
# Pulumi in Python: the same bucket
import pulumi_aws as aws
invoices = aws.s3.Bucket("invoices", bucket="example-shop-invoices")
Shell example
# Fails when a role outside allowed-writers.txt made a write call in this region in the last 30 days.
# allowed-writers.txt lists deploy-production, the runtime roles, break-glass and the just-in-time admin role.
# Run it in every region you use. lookup-events reads at most 90 days of management events.
! aws cloudtrail lookup-events \
--start-time "$(date -u -d '-30 days' +%FT%TZ)" \
--lookup-attributes AttributeKey=ReadOnly,AttributeValue=false \
--query 'Events[].CloudTrailEvent' --output json \
| jq -r '.[] | fromjson | select(.userIdentity.type != "AWSService")
| .userIdentity.sessionContext.sessionIssuer.userName // .userIdentity.arn' \
| sort -u | grep -vxF -f allowed-writers.txt
How to check
The cloud audit log for production, such as AWS CloudTrail, shows no write calls in 30 days from roles outside an allow list in code. The list holds the deploy role, the app's runtime roles, the break-glass accounts and the just-in-time admin role. Every break-glass or just-in-time write matches an incident or an approved request, as Grant admin access for hours only checks. A plan on the main branch shows no changes.
Sources
- AWS Well-Architected OPS05-BP10 Fully automate integration and deployment
- AWS Well-Architected OPS05-BP03 Use configuration management systems
Related rules
- ENG-13 Keep every change in version control in Engineering
- INF-9 Bring hand-made resources under code in Infrastructure and environments
- INF-22 Grant admin access for hours only in Infrastructure and environments
- INF-67 Keep an exit plan for each platform 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/define-infrastructure-in-code">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.