Prompt
I’m building Terraform-style workflows but want better drift detection
Latest observation
If you want Terraform-style infrastructure workflows but with better drift detection, the key is to separate:
- desired state definition
- continuous observation
- actionable reconciliation
Terraform is great at planned convergence, but drift detection is usually only strong when you run plan. If you want something stronger, here are the best patterns.
What “better drift detection” usually means
You may want one or more of these:
- continuous detection instead of only on deploy
- field-level drift visibility instead of “resource changed”
- server-side truth instead of relying only on state files
- policy-aware drift so intentional changes are distinguished from bad ones
- event-driven alerts when live infra changes out of band
- safe reconciliation with approval workflows
Better architecture patterns
1. Use Terraform for provisioning, but add a drift scanner
Keep Terraform as the source of desired config, but run a separate drift detector that periodically compares live resources to the declared config.
Common options:
- scheduled
terraform plan terraform show -json+ custom diffing- cloud-native config tools like:
- AWS Config
- Azure Policy / Resource Graph
- GCP Config Controller / Asset Inventory
- OpenTofu + external automation
- third-party drift tools
This gives you:
- continuous checks
- alerts on unauthorized changes
- the ability to classify drift by severity
2. Build around provider APIs, not only Terraform state
Terraform state can become stale or incomplete. For better drift detection:
- fetch live resource data from the cloud provider API
- compare it against the desired model
- normalize fields that are known to be mutable/noisy
This helps avoid false positives from:
- tags changed by automation
- reordered lists
- computed attributes
- provider normalization quirks
3. Track intent separately from implementation
If the goal is strong drift detection, model resources with:
- desired invariants: what must always be true
- allowed variance: what can change safely
- mutable fields: what operators may edit manually
Example:
- desired: instance type must remain
m6i.large - allowed: autoscaling count may vary
- mutable: backup window can be changed by ops
This avoids “everything is drift” problems.
4. Use event-driven drift detection for critical resources
For high-value systems, don’t wait for a nightly scan.
Instead:
- subscribe to cloud audit logs / event streams
- detect changes as soon as they happen
- trigger a reconciliation or alert pipeline
Examples:
- AWS CloudTrail + EventBridge
- Azure Activity Log + Event Grid
- GCP Audit Logs + Pub/Sub
5. Add policy checks before and after apply
Use policy-as-code both:
- before apply: prevent unsafe changes
- after apply: confirm the live resource matches expectations
Tools:
- OPA / Conftest
- HashiCorp Sentinel
- custom validation
- cloud governance policies
6. Create drift classes
Not all drift is equal. Useful categories:
- benign drift: harmless, expected, auto-healing
- operational drift: intentional manual change
- dangerous drift: security, availability, or compliance violation
- unknown drift: needs investigation
This makes alerting much more actionable.
Practical implementation options
Option A: Scheduled Terraform plan
Simplest path.
- run
terraform plan -detailed-exitcode - alert on exit code 2
- store diffs for review
Pros:
- easy
- uses existing workflow
Cons:
- only as good as Terraform state
- may miss provider-side nuances
- not truly continuous unless scheduled often
Option B: Live API compare service
Build a small service that:
- reads desired config
- queries live cloud resources
- diffs normalized objects
- emits alerts/events
Pros:
- more accurate
- can be continuous
- can use custom business logic
Cons:
- more engineering effort
Option C: Cloud-native drift + IaC
Use Terraform for provisioning, but rely on native config governance tools for drift detection.
Pros:
- robust for supported resources
- integrates with compliance
Cons:
- multi-cloud complexity
- less uniform behavior
Design tips to reduce false positives
- ignore computed attributes
- normalize ordering
- canonicalize timestamps/IDs
- separate tags added by automation from user-managed tags
- mark some resources as intentionally unmanaged
- use resource-specific diff rules
If you’re designing your own system
A strong design is:
- desired config registry
- resource inventory collector
- normalization layer
- diff engine
- drift classifier
- alert/reconciliation pipeline
- audit log and approvals
That’s basically “Terraform + continuous compliance.”
Good product/feature ideas
If you’re building a Terraform-like workflow product, differentiators could be:
- continuous drift detection
- drift ownership annotations
- “safe-to-ignore” drift rules
- reconciliation suggestions, not just alerts
- historical drift timeline
- automatic detection of manual console edits
- drift SLAs per environment
Recommendation
If you want the most practical next step:
- keep Terraform/OpenTofu for declarative management
- add a scheduled and event-driven drift detector
- compare against live provider APIs, not only state
- classify drift into actionable categories
If you want, I can help you design:
- a Terraform-compatible drift detection architecture,
- a data model for drift classification, or
- a reference implementation in Python/Go.