Your infrastructure-as-code repo says the production VPC has two NAT gateways. The console shows three. The third was added during an incident eight months ago and nobody updated the Terraform. Multiply that across a few teams over a year and the code is no longer a description of reality — it is a fiction that diverges every time someone solves a problem with a click. Config drift detection is the practice of noticing when this has happened, ideally before the next emergency assumes the code is true. This article covers the tools and the schedule.
How to verify
For each layer of the infrastructure, there is a different drift signal:
# Terraform: what would apply do?
terraform plan -detailed-exitcode
echo "exit was $?" # 0 = no change, 2 = drift
# Ansible: what would the playbook change?
ansible-playbook -i inventory/production site.yml --check --diff | tee /tmp/ansible-check.log
grep -c '^changed:' /tmp/ansible-check.log
# AIDE / file integrity
sudo aide --check | head -50
# AWS Config / cloud-native
aws configservice describe-compliance-by-resource --compliance-types NON_COMPLIANT
terraform plan -detailed-exitcode is the cleanest signal: exit 0 means clean, exit 2 means drift. Wire that into the daily check.
What’s happening
Drift happens at multiple layers. Cloud resources drift when an operator edits a security group rule in the console. Operating-system configuration drifts when someone SSHes in and edits /etc/nginx/nginx.conf to fix something for a customer at 2 a.m. Application configuration drifts when a feature flag is flipped via a backdoor API. File integrity drifts when a system file is modified by a process that should not be modifying system files — drift detection at this layer is also intrusion detection.
The drift detection toolset matches the layer: Terraform plan for cloud-API-managed resources; Ansible check mode for OS-level config; AIDE, Tripwire, or auditd for file integrity; cloud-native services (AWS Config, Azure Policy, GCP Asset Inventory) for compliance posture. The point is not to prevent drift — preventing drift entirely is impossible in any organization that runs incidents — but to surface it quickly and either reconcile (update the code) or revert (re-apply from code).
The procedure
-
Schedule a daily Terraform plan job. A CI job runs
terraform plan -detailed-exitcodeagainst every state file daily and posts the diff (or a summary) to a shared channel. Exit code 2 fires an alert.# .github/workflows/drift.yml on: schedule: - cron: '0 7 * * *' jobs: plan: strategy: matrix: env: [production, staging] component: [network, data, app] steps: - uses: actions/checkout@v4 - uses: hashicorp/setup-terraform@v3 - run: terraform -chdir=infra/${{ matrix.env }}/${{ matrix.component }} init - run: | terraform -chdir=infra/${{ matrix.env }}/${{ matrix.component }} plan -detailed-exitcode -no-color -
Schedule an Ansible check job. The same shape, against the inventory.
ansible-playbook -i inventory/production site.yml --check --diff > /tmp/ansible-drift.log if grep -q 'changed: \[' /tmp/ansible-drift.log; then # post diff to channel + raise ticket fi -
Wire AIDE for host-level file integrity. AIDE compares a snapshot of file metadata + hashes against a baseline. Run it daily via systemd timer.
sudo apt install -y aide sudo aideinit sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db# /etc/systemd/system/aide-check.service [Unit] Description=AIDE file integrity check [Service] Type=oneshot ExecStart=/usr/bin/aide --check --config /etc/aide/aide.conf StandardOutput=journal StandardError=journal -
Enable cloud-native compliance services. AWS Config records every resource change; Conformance Packs codify rules. Findings flow into Security Hub. The same idea exists on Azure (Policy) and GCP (Asset Inventory + SCC).
-
Reconcile or revert. When drift is found, the question is “is the live state correct and the code stale, or is the live state wrong and the code correct?”. The answer determines whether you update the IaC to match reality (reconcile) or reapply the code (revert). Both need a PR — neither happens by silent re-apply.
-
Run drift detection in PR mode for sensitive changes. Some teams set “auto-reconcile on PR merge” via Atlantis or Terraform Cloud. The drift detection becomes the trigger for a PR rather than a manual ticket.
Common pitfalls
- A
terraform planagainst the wrong workspace shows the wrong drift; pin the workspace in CI. - Drift on a resource Terraform doesn’t manage (an unmanaged security group rule, for example) won’t be detected —
lifecycle { ignore_changes = [...] }is a footgun if used to silence drift instead of declaring the resource. - Ansible check mode for tasks that use
command:orshell:returns conservatively “changed” — fine if you read the diff, noisy if you rely on the headline count. - AIDE on a host with regular config rotation (logs, package updates) needs a clear exclusion list or it cries wolf every run.
- A drift report nobody looks at is worse than no drift report — the team becomes habituated to red status and ignores the real findings.
In the engagements we run, daily drift detection is part of the operations rhythm — Terraform plan against every workspace, Ansible check against every host, AIDE on the systems where it matters, and a weekly review where drift findings are either reconciled into the code or reverted. The cumulative effect is that the IaC stays trustworthy. See /en/services/managed-operations/.