Skip to main content

IaC Drift Detection Patterns Guide

Infrastructure drift is the silent killer of IaC reliability. This guide covers what drift is, how to detect it across Terraform, Pulumi, CDK, and Ansible, and how to build a prevention workflow that keeps your cloud and your code in sync.

✓ Detection methods compared ✓ Interactive scenario explorer ✓ Prevention checklist ✓ Remediation workflow ✓ Platform recommendations

What is IaC Drift?

Infrastructure drift is any difference between the state your IaC tool believes the world is in and what the cloud provider actually has. Drift can be as minor as a tag value changing, or as severe as an entire database instance being deleted or a security group rule being added that opens a critical port.

🖱️

Manual Console Changes

An engineer opens the AWS Console and adds an ingress rule to a security group during an incident. The rule is never removed. Terraform's state file still shows the old rule.

⚖️

Auto-scaling & Cloud Events

AWS Auto Scaling adds or removes EC2 instances. RDS creates read replicas automatically. These cloud-managed changes are expected drift that IaC should be configured to ignore.

🔀

Parallel Tool Changes

Another team uses a different IaC tool (CloudFormation, Ansible) or a deployment pipeline that modifies shared resources — VPCs, IAM roles, Route53 records — without updating your state.

Why drift is dangerous: The next terraform apply may silently revert a critical fix, delete resources that were manually added, or make the plan output unpredictable — turning a routine deploy into a production incident.

Drift Detection Methods Compared

Tool / Method What It Detects Scope Speed Unmanaged Resources Best For
terraform plan Config vs state drift State-tracked resources only Medium No Daily CI drift check
terraform apply -refresh-only Real cloud vs state State-tracked resources only Medium No State reconciliation
driftctl All cloud resources vs IaC Entire AWS account Slow (full scan) Yes Coverage & compliance
Infracost Cost drift from IaC changes Config-level cost impact Fast Partial PR cost review
pulumi refresh Real cloud vs Pulumi state Stack-tracked resources Medium No Pulumi stack sync
AWS Config Rules Compliance violations All AWS resources Continuous Yes Policy enforcement
Custom scripts (AWS CLI) Specific resource attributes Targeted Fast (targeted) If scripted Custom business rules

Drift Scenario Explorer

Select a scenario to see what drift it causes and how to detect and remediate it.

Click a scenario above to explore drift details.

IaC Drift Prevention Checklist

Check off the practices your team has implemented. Your score updates live.

Prevention score
0 / 14

Access Control

State Management

CI/CD Integration

Monitoring & Alerting

Culture & Process

Drift Alert Thresholds (UI Mock)

Configure what constitutes critical vs minor drift for your environment. Adjust sliders to see recommended alert levels.

Ingress/egress rules added or removed outside IaC

Role/policy attachments modified directly

Auto-scaling changes to fleet size

Bucket ACLs or policies modified manually

Records added or modified outside IaC

Backup retention, parameter groups, read replicas

Your Drift Alert Configuration Summary

    Remediation Workflow

    When drift is detected, follow this structured decision flow.

    Step 1
    Detect
    • • CI plan shows diff
    • • driftctl scan alert
    • • AWS Config violation
    • • CloudTrail audit hit
    ↓
    Step 2
    Assess
    • • Who made the change?
    • • Is it intentional?
    • • Security impact?
    • • Data at risk?
    ↓
    Step 3
    Decide
    • A: Update IaC to match
    • B: Apply to revert cloud
    • C: Add ignore_changes
    • D: Escalate to security
    ↓
    Step 4
    Reconcile
    • • Open PR with fix
    • • Run plan → review
    • • Apply via CI/CD
    • • Document incident
    ↓
    Step 5
    Verify
    • • Plan shows no diff
    • • driftctl shows 100%
    • • Config Rules pass
    • • Add to post-mortem
    A — Update IaC: The change was intentional (e.g. an approved emergency fix). Codify it in IaC so it's permanent and tracked.
    B — Revert cloud: The change was unauthorized or accidental. Run terraform apply to restore IaC-defined state.
    C — Ignore changes: The drift is expected (auto-scaling). Add lifecycle { ignore_changes } to prevent false alerts.
    D — Escalate: The change looks like a security breach. Engage your security team; isolate resources before modifying anything.

    Tool Recommendations by IaC Platform

    Select your IaC platform above for tailored tool recommendations.

    Frequently Asked Questions

    What is IaC drift and why is it dangerous?▼

    IaC drift occurs when your live cloud infrastructure diverges from what your Infrastructure-as-Code configuration describes. It's dangerous for several reasons: it breaks the predictability of IaC (your 'source of truth' no longer reflects reality), it makes the next automated deployment unpredictable or destructive, it can introduce security vulnerabilities (e.g. a security group rule opened manually), and it erodes team trust in automation. Even small drifts compound over time — a manually added S3 bucket policy today becomes an unexpected terraform destroy next sprint.

    What is the difference between terraform plan, terraform refresh, and driftctl?▼

    These three tools detect drift at different scopes. 'terraform plan' is the standard approach: it compares your config against the state file and shows what Terraform would change — but it only knows about resources tracked in your state file. 'terraform refresh' (deprecated in TF 1.x, replaced by 'terraform apply -refresh-only') updates the state file to match actual cloud reality without making changes, which lets you see what drifted. Driftctl is a dedicated open-source drift scanner that goes further: it detects cloud resources that exist in your account but are NOT in any Terraform state at all — so-called 'unmanaged resources'. This is the coverage driftctl excels at.

    How do I prevent IaC drift in production?▼

    Prevention has four pillars. First, enforce IaC-only changes: use IAM policies and OPA/Sentinel rules to block direct console edits in production accounts. Second, run drift detection in CI/CD: schedule 'terraform plan' or driftctl scans daily or on every push, and alert on any diff. Third, use state locking with DynamoDB (Terraform) or Pulumi Cloud to prevent concurrent modifications. Fourth, implement GitOps: treat your IaC repo as the only source of truth, and use automated reconciliation (ArgoCD, Atlantis, Flux) so any deviation triggers an automatic correction. Teams that skip even one pillar typically see drift accumulate within weeks.

    What should I do when I detect drift — fix the IaC or apply a terraform apply?▼

    The decision depends on why the drift happened. If someone made an emergency fix that should be permanent (e.g., opening a port during an incident), update the IaC to match, commit, and run a plan/apply to reconcile. If the drift is unintentional or unauthorized (someone clicked around in the console), run 'terraform apply' to revert cloud to match IaC — after confirming no data loss. If it's auto-scaling state (instance counts, read replicas), consider using 'lifecycle { ignore_changes }' to tell Terraform to ignore those attributes. Never just 'terraform refresh' and commit the drifted state without understanding why the drift occurred.

    Which drift detection tool should I use for Terraform, Pulumi, CDK, and Ansible?▼

    For Terraform: start with 'terraform plan -refresh-only' built-in, add driftctl for unmanaged resource detection, and Infracost for cost drift. For Pulumi: 'pulumi refresh' updates stack state and shows drift; Pulumi Cloud also provides drift reports. For AWS CDK: use 'cdk diff' which shows CloudFormation change sets — CloudFormation natively tracks drift via 'aws cloudformation detect-stack-drift'. For Ansible: use '--check' mode (dry-run) to see what would change; ansible-lint and Molecule help validate configuration state. For all platforms, AWS Config Rules provide a compliance layer that continuously evaluates resources against your policies regardless of which IaC tool manages them.

    Share this tool: X / Twitter LinkedIn
    ✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

    Related Products

    on Gumroad

    You Might Also Need

    More Free Tools

    Enjoying this tool? Get more like it.

    Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

    DevPlaybook Pro 7 days free

    Unlock AI-Powered Dev Tools

    • ⚡ AI Code Review, Doc Generator & SQL Builder
    • ⚡ All premium templates & early access
    • ⚡ Member discounts on Gumroad products
    Start Free Trial →

    $9/mo after trial · Cancel anytime

    Want the full toolkit?

    Get DevPlaybook Pro

    Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

    $79
    $241 separately Save $162 (67% off)
    Get DevPlaybook Pro — $79 →

    MIT licensed · Instant download · No subscription

    See what's included → Browse all deals →