Audit Infra Drift
Detect and remediate differences between infrastructure-as-code declarations and the actual state of deployed resources.
Why This Is Best Practice
Adopted by: WeaveWorks (GitOps origin), AWS (Config + CloudFormation drift detection), HashiCorp (Terraform plan as drift detection)
Impact: Puppet's State of DevOps report found that IaC adoption reduces change failure rate by 60%; undetected drift is the #1 cause of "works in staging, fails in production" incidents.
Why best: Drift accumulates when engineers make manual ("click-ops") changes to production that are not reflected in the IaC source of truth. Over time, the declared state diverges from reality, making changes unpredictable and compliance verification impossible. Regular drift audits restore confidence in IaC as the authoritative source.
Steps
- Run a plan/diff against current state — Execute
terraform plan (Terraform), kubectl diff (Kubernetes), or aws cloudformation detect-stack-drift to surface all differences between declared and actual state.
- Classify each drift item — Authorized drift (intentional manual override, documented), unauthorized drift (undocumented manual change), configuration skew (environment-specific differences).
- Trace drift origin — Check CloudTrail / audit logs to identify who made the manual change and when. This determines remediation urgency and process improvement.
- Remediate by reconciling IaC — Either: (a) update IaC to reflect the correct desired state and apply, or (b) revert the manual change via IaC apply. Never reconcile by making more manual changes.
- Enforce GitOps controls — Remove manual IAM permissions for production resource modification where possible; all changes must flow through the IaC pipeline.
- Schedule continuous drift detection — Run
terraform plan in CI on a schedule (e.g., nightly); alert on non-empty plans. Use AWS Config rules for real-time compliance checks.
- Document exceptions — For authorized drift (e.g., auto-scaling changes managed by AWS), configure Terraform
ignore_changes or AWS Config suppressions to exclude from alerts.
Rules
- A non-empty
terraform plan on a production environment is an incident signal — investigate before the next deploy.
- All production changes must be traceable to a git commit; manual changes are a process violation, not a solution.
- Drift in security-sensitive resources (IAM policies, security groups, KMS) triggers immediate remediation regardless of authorization status.
- Use remote state locking (S3 + DynamoDB, Terraform Cloud) to prevent concurrent state modification.
Examples
# Terraform drift detection
terraform plan -detailed-exitcode
# Exit code 2 = changes detected (drift present)
# AWS Config drift detection
aws cloudformation detect-stack-drift --stack-name production-stack
aws cloudformation describe-stack-resource-drifts --stack-name production-stack \
--stack-resource-drift-status-filters MODIFIED DELETED
Common Mistakes
- Reconciling drift with more manual changes — compounds the problem; always go through IaC.
terraform apply -auto-approve without reviewing the plan — applies unintended drift removals that may cause outages.
- No drift detection schedule — drift discovered at deployment time is far more disruptive than drift caught nightly.
1---2name: audit-infra-drift3description: Use when detecting, auditing, or remediating drift between declared infrastructure configuration and actual deployed state4source: GitOps Principles (opengitops.dev); HashiCorp Terraform documentation; AWS Config documentation5tags: [infrastructure, drift, terraform, gitops, aws-config, iac, devops]6verified: true7metadata:8 source: https://skillmd.com/skills/jeffreytse/audit-infra-drift9---1011# Audit Infra Drift1213Detect and remediate differences between infrastructure-as-code declarations and the actual state of deployed resources.1415## Why This Is Best Practice1617**Adopted by:** WeaveWorks (GitOps origin), AWS (Config + CloudFormation drift detection), HashiCorp (Terraform plan as drift detection)18**Impact:** Puppet's State of DevOps report found that IaC adoption reduces change failure rate by 60%; undetected drift is the #1 cause of "works in staging, fails in production" incidents.1920**Why best:** Drift accumulates when engineers make manual ("click-ops") changes to production that are not reflected in the IaC source of truth. Over time, the declared state diverges from reality, making changes unpredictable and compliance verification impossible. Regular drift audits restore confidence in IaC as the authoritative source.2122## Steps23241. **Run a plan/diff against current state** — Execute `terraform plan` (Terraform), `kubectl diff` (Kubernetes), or `aws cloudformation detect-stack-drift` to surface all differences between declared and actual state.252. **Classify each drift item** — Authorized drift (intentional manual override, documented), unauthorized drift (undocumented manual change), configuration skew (environment-specific differences).263. **Trace drift origin** — Check CloudTrail / audit logs to identify who made the manual change and when. This determines remediation urgency and process improvement.274. **Remediate by reconciling IaC** — Either: (a) update IaC to reflect the correct desired state and apply, or (b) revert the manual change via IaC apply. Never reconcile by making more manual changes.285. **Enforce GitOps controls** — Remove manual IAM permissions for production resource modification where possible; all changes must flow through the IaC pipeline.296. **Schedule continuous drift detection** — Run `terraform plan` in CI on a schedule (e.g., nightly); alert on non-empty plans. Use AWS Config rules for real-time compliance checks.307. **Document exceptions** — For authorized drift (e.g., auto-scaling changes managed by AWS), configure Terraform `ignore_changes` or AWS Config suppressions to exclude from alerts.3132## Rules3334- A non-empty `terraform plan` on a production environment is an incident signal — investigate before the next deploy.35- All production changes must be traceable to a git commit; manual changes are a process violation, not a solution.36- Drift in security-sensitive resources (IAM policies, security groups, KMS) triggers immediate remediation regardless of authorization status.37- Use remote state locking (S3 + DynamoDB, Terraform Cloud) to prevent concurrent state modification.3839## Examples4041```bash42# Terraform drift detection43terraform plan -detailed-exitcode44# Exit code 2 = changes detected (drift present)4546# AWS Config drift detection47aws cloudformation detect-stack-drift --stack-name production-stack48aws cloudformation describe-stack-resource-drifts --stack-name production-stack \49 --stack-resource-drift-status-filters MODIFIED DELETED50```5152## Common Mistakes5354- **Reconciling drift with more manual changes** — compounds the problem; always go through IaC.55- **`terraform apply -auto-approve` without reviewing the plan** — applies unintended drift removals that may cause outages.56- **No drift detection schedule** — drift discovered at deployment time is far more disruptive than drift caught nightly.