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.
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.
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.
What Happened
Drift Caused
Detect & Fix
Recommended Command
Click a scenario above to explore drift details.
IaC Drift Prevention Checklist
Check off the practices your team has implemented. Your score updates live.
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.
- • CI plan shows diff
- • driftctl scan alert
- • AWS Config violation
- • CloudTrail audit hit
- • Who made the change?
- • Is it intentional?
- • Security impact?
- • Data at risk?
- A: Update IaC to match
- B: Apply to revert cloud
- C: Add ignore_changes
- D: Escalate to security
- • Open PR with fix
- • Run plan → review
- • Apply via CI/CD
- • Document incident
- • Plan shows no diff
- • driftctl shows 100%
- • Config Rules pass
- • Add to post-mortem
terraform apply to restore IaC-defined state. lifecycle { ignore_changes } to prevent false alerts. Tool Recommendations by IaC Platform
Primary Drift Detection
Complementary Tools
Quick Start Command
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.
Related Products
on GumroadPremium Workflow Pack
Production-grade workflows, architecture guides, and deployment scripts for teams that ship. 10 advanced CI/CD workflows (blue-green, canary, E2E, security), 5 architecture deep-dives, code review checklist, PR process guide, and 5 battle-tested Bash deployment scripts.
Buy on Gumroad →GitHub Actions CI/CD Templates Pack
12 production-ready GitHub Actions workflow templates covering Node.js, Python, Docker, multi-environment deployments, security scanning, and auto-changelog. Copy, paste, ship.
Buy on Gumroad →Developer Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →You Might Also Need
Terraform Variable Builder
Build Terraform variable declarations for variables.tf with a visual form. Set type, default value, description, sensitivity, and validation rules.
Open free tool →
Terraform Variable Validator
Validate your Terraform variables.tf online. Catches missing type constraints, undocumented variables, unmasked secrets, and type/default mismatches. Runs 100% in your browser.
Open free tool →
Ansible Playbook Builder
Build Ansible playbooks visually with a task composer. Add apt, yum, copy, service, command, git, and other module tasks. Generate valid YAML playbooks.
Open free tool →
More Free Tools
Related Articles
Pulumi vs Terraform 2026: Which IaC Tool Should You Choose?
A deep comparison of Pulumi and Terraform for infrastructure as code in 2026. Covers syntax, state management, provider ecosystem, team adoption, and when to choose each tool.
Terraform vs Pulumi vs AWS CDK: Infrastructure as Code Comparison 2026
Complete comparison of Terraform, Pulumi, and AWS CDK for infrastructure as code in 2026. Covers language support, state management, learning curve, ecosystem, cloud support, pricing, and when to use each tool.
Terraform vs Pulumi 2026: Infrastructure as Code Comparison with Code Examples
Complete Terraform vs Pulumi comparison for 2026 — syntax, state management, multi-cloud support, testing, pricing, and when to choose each IaC tool.
AWS CDK vs Terraform vs Pulumi: Infrastructure as Code Comparison 2026
Complete comparison of AWS CDK, Terraform, and Pulumi for infrastructure as code in 2026 — language support, state management, multi-cloud, community, and when to use each.
Terraform vs Pulumi: Infrastructure as Code Comparison 2026
In-depth comparison of Terraform and Pulumi for Infrastructure as Code in 2026. Covers HCL vs real programming languages, state management, provider ecosystem, and when to use each.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$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.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →