Lesson 8 of 9 · 24m
Drift Detection and Reconciliation Without Superstition
Detect out-of-band change, decide whether to adopt, correct, or tolerate it, and build drift operations that do not destroy intentional runtime behavior.
Drift is not automatically evil. Sometimes drift is an emergency change that saved the business at 2 a.m. Sometimes it is a console experiment nobody admitted to. Sometimes it is a cloud provider mutating defaults behind your back. The job is not to erase all differences blindly. The job is to make differences visible and decide deliberately which source of truth wins.
Understand what Terraform refresh can see
Terraform refreshes managed resources before planning, but it does not discover unmanaged objects and it cannot infer intent. If someone changed a security group rule under Terraform management, plan may show it:
Note: Objects have changed outside of Terraform
# aws_security_group_rule.ssh has changed
~ resource "aws_security_group_rule" "ssh" {
~ cidr_blocks = ["10.0.0.0/8"] -> ["0.0.0.0/0"]
}That output is evidence, not a verdict. Did the incident response team open access? Was it a debugging mistake? Is a provider normalizing data? Investigate before applying away the difference.
Use refresh-only plans for inspection
A refresh-only plan records external changes without proposing the corrective configuration path:
terraform plan -refresh-onlyThis helps answer 'what changed?' before asking 'what would Terraform do about it?' In automation, run it read-only, alert on differences, and require human triage for critical resources.
Scheduled drift checks need scope discipline
A simple CI schedule might run:
terraform init -input=false
terraform plan -refresh-only -detailed-exitcode -out=drift.tfplanExit code 0 means no difference, 1 means error, and 2 means changes detected. Do not run blanket auto-apply on drift. You may overwrite emergency fixes, autoscaling decisions, or provider-managed behavior.
Some fields should be ignored — consciously
Cloud systems mutate operational fields: scaling counts, health-check state, certificate metadata, ETag-like values, and managed tags. Use lifecycle rules when the platform genuinely owns a field:
resource "aws_autoscaling_group" "workers" {
desired_capacity = var.initial_capacity
lifecycle {
ignore_changes = [desired_capacity]
}
}This is appropriate when a cluster autoscaler owns desired capacity. It is dangerous when used to silence disagreement nobody understands. Every ignore_changes entry should have a reason in code review.
Reconciliation has three legitimate outcomes
First, correct the infrastructure because Terraform remains authoritative. Second, adopt reality by changing configuration because the manual change was intentional. Third, remove Terraform ownership because another controller owns that field or resource. Do not confuse these paths:
correct -> terraform apply reviewed corrective plan
adopt -> edit HCL/variables until plan is clean
delegate -> state rm, lifecycle ignore, or controller boundary changePrevent drift where permissions allow
Drift detection is weaker than drift prevention. Restrict console write access for production roles, require break-glass accounts with audit alarms, and route automation through the same delivery pipeline. Humans will still make emergency changes; the system should make those changes visible rather than impossible.
Field notes
- Provider upgrades can create phantom drift when schemas or defaults are normalized differently. Read provider changelogs before 'fixing' cloud resources.
- A nightly drift job that writes state but is never reviewed is observability theater.
- Immutable infrastructure drifts less; mutable long-lived snowflakes drift constantly.
- Drift in data resources and unmanaged objects will not appear just because you ran Terraform.
- Record emergency manual changes in the same ticket system as Terraform work so reconciliation has context.
Was this lesson helpful?