Drift Detection Pipeline for IaC
Overview
This architecture designs a scalable, automated drift detection pipeline for Infrastructure as Code (IaC), focusing on tools like Terraform (extensible to CloudFormation, Pulumi, etc.). The pipeline runs periodically (e.g., via cron/scheduler) or event-driven (e.g., post-merge in GitOps). It detects configuration drift—divergences between declared IaC state and live infrastructure—while incorporating suppression rules to reduce noise and an incident flow for reliable response.
Key goals:
- Early detection: Catch manual changes, API drift, or external updates.
- Low noise: Suppression minimizes false positives.
- Actionable incidents: Integrate with ITSM tools for remediation.
- Cloud-agnostic: Examples use AWS/Terraform; adapt via providers.
High-level Pipeline Flow:
IaC Sources → State Extraction → Drift Comparison → Suppression → Incident Flow → Remediation
Tech Stack:
- Orchestration: Apache Airflow, AWS Step Functions, or GitHub Actions.
- Drift Detection: driftctl (open-source, multi-provider) or Terraform's
plan + show-json.
- Storage: S3/GCS for artifacts; DynamoDB/Redis for suppression rules.
- Alerting: PagerDuty/Slack + Jira/ServiceNow for incidents.
- Scheduling: Lambda + EventBridge (AWS) or Cloud Scheduler (GCP).
1. Sources
Sources provide desired state (from IaC) and actual state (live infra). Pipeline fetches atomically to avoid TOCTOU issues.
Desired State Sources
| Source Type | Description | Fetch Mechanism | Frequency |
|---|
| Git Repos | Primary IaC (Terraform modules, .tf files). Supports mono/multi-repo. | Git clone/pull via GitHub/GitLab API. Webhooks for post-merge triggers. | On-commit, daily. |
| State Files | terraform.tfstate (remote: S3/Consul). | terraform state pull or API (e.g., aws s3 cp). | Per-run. |
| Policy-as-Code | OPA/Conftest policies defining "golden" configs. | Git fetch + bundle. | On-change. |
- Extraction: Run
terraform init && terraform plan -out=plan.tfplan -detailed-exitcode=1 && terraform show -json plan.tfplan > desired.json.
Actual State Sources
| Provider | Description | Fetch Mechanism | Auth |
|---|
| AWS | EC2, S3, VPCs, etc. | driftctl scan --from terraform.tfstate --to aws+tf or AWS Config API. | IAM role w/ ReadOnlyAccess. |
| Azure | Resources via ARM. | az resource list or driftctl azure+tf. | Service Principal. |
| GCP | Compute Engine, etc. | gcloud assets list or driftctl gcp+tf. | Service Account. |
| Multi-Cloud | Kubernetes (via K8s API), on-prem (via providers). | driftctl supports 20+ providers. | Provider-specific. |
- Extraction:
driftctl scan --from <source> --to <provider> --output drift.json.
- Snapshot Storage: Persist
desired.json + actual.json in S3 with versioning for audits.
2. Suppression Rules
Suppression filters known/approved drifts (e.g., auto-scaling groups, temp resources) to achieve <5% false positives. Rules are declarative, versioned, and auditable.
Rule Engine
- Format: YAML/JSON config stored in Git/S3, loaded via ConfigMap.
- Evaluation: Custom script (Python/Go) or driftctl's
--exclude + OPA for complex logic.
- Rule Types:
| Type | Syntax Example | Use Case |
|---|
| Resource Ignore | ignore: resources: [aws_instance.web, module.db.*] | Ephemeral resources (e.g., spot instances). |
| Attribute Ignore | ignore: attributes: [tags.admin_email, last_modified] | Dynamic fields (timestamps, auto-tags). |
| Tag-Based | ignore: if: resource.tags.drift_suppress == "true" | Manual opt-out (with expiry). |
| Regex/Path | ignore: path_regex: "^/spec/replicas$" | K8s scaling. |
| Threshold | warn_only: if: diff_size < 10 | Minor drifts. |
| Time-Based | ignore: until: 2024-12-31 | Temporary changes. |
Implementation
# suppression-rules.yaml (Git-managed)
version: 1
rules:
- name: ignore-ephemeral-tags
match:
provider: aws
resource: aws_autoscaling_group
ignore:
attributes: [^.+\.last_updated$]
- name: db-password-rotated
match:
resource: aws_rds_cluster
attribute: master_password
ignore: true # Or until: "expiry-date"
3. Incident Flow
Drift → Evaluated Alert → Incident with auto-remediation options. SLO: Detect in <5min, alert in <1min.
Flow Diagram
Drift Detected?
↓ No → Exit (log metric)
↓ Yes
Apply Suppression?
↓ No Match → Create Incident
↓ Match → Log & Metric Only
Incident:
1. Enrich (diff details, owner from tags)
2. Notify (Slack/PagerDuty)
3. Create Ticket (Jira)
4. Auto-Remediate? → Drift Fix PR
5. Escalate/Monitor
Detailed Steps
| Step | Action | Tools/Outputs |
|---|
| 1. Detection | Post-comparison: If diff_count > 0, generate report. | drift.json + HTML diff (driftctl --output html). |
| 2. Enrichment | Attach: Resource owner (from tags), severity (critical/high/medium based on type), diff snippet. | Python lambda: Parse JSON → Structured payload. |
| 3. Alerting | Immediate notify. | PagerDuty (P1 for prod), Slack channel. |
| 4. Incident Creation | Auto-ticket with playbook. | Jira: "IaC Drift: aws_s3_bucket.prod-public (owner: infra-team)". |
| 5. Remediation Paths | - Auto: GitHub Action creates PR with terraform apply plan.- Manual: Link to self-service portal (re-run plan).- Prevent: Webhook blocks deploys on open drifts. | Atlantis/Atlanter for PRs; approval gates. |
| 6. Resolution & Close | Monitor: Re-scan on ticket close. Auto-close if drift fixed. | Webhook from Jira → Pipeline trigger. |
| 7. Post-Mortem | Weekly report: Top drifts, suppression efficacy. | Grafana dashboard (drifts/hour, MTTR). |
Severity Tiers
- Critical: Security/resources (e.g., public S3).
- High: Compute/network changes.
- Medium: Config/tags.
Metrics & Observability
- Dashboards: Drift count, suppression %, MTTR (Prometheus/Grafana).
- Costs: ~$0.01/run (API calls); scale with parallelism.
Deployment & Scaling
- Quickstart: AWS Lambda + EventBridge (cron every 6h).
- Enterprise: Airflow DAG with fan-out for accounts/regions.
- Extensibility: Plugin for new providers; integrate with Backstage for portals.
This pipeline ensures proactive IaC hygiene, reducing toil by 80%+ in mature teams. For implementation code/repos, provide your IaC flavor/cloud!