INF-7Recommended
Detect drift every day
Run a plan against every state at least once a day. Open a ticket when it finds a change nobody made in code.
How to do it
Run a scheduled CI job with the read-only plan role. plan -detailed-exitcode exits with 0 for no changes, 1 for an error and 2 for changes. Treat 2 as drift. Within 2 working days, either write the change into code or apply the code to undo it. Tools such as Spacelift, Atlantis or HCP Terraform offer the same as a service. To keep a hand-made change, follow Bring hand-made resources under code.
Examples
YAML example
on:
schedule: [{ cron: '17 5 * * *' }]
jobs:
drift:
runs-on: ubuntu-latest
strategy:
matrix: { stack: [staging/network, staging/app, production/network, production/app] }
steps:
- uses: actions/checkout@<commit-sha>
- run: |
cd infra/${{ matrix.stack }} && tofu init -lockfile=readonly
tofu plan -detailed-exitcode -lock=false || { code=$?; [ $code -eq 2 ] && ./open-drift-ticket.sh "${{ matrix.stack }}"; exit $code; }
How to check
The drift job ran in the last 24 hours for every state. Change a tag by hand on a staging resource, and a ticket opens within 24 hours.
Sources
- AWS Well-Architected REL08-BP04 Deploy using immutable infrastructure
- AWS Well-Architected OPS05-BP03 Use configuration management systems
Related rules
- INF-9 Bring hand-made resources under code 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/drift-detection">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.