Course map

Topic 185 / Advanced

Infrastructure as code (Terraform as the example): infrastructure drift

Chapter 11.4 · Infrastructure as Code

What and why

Infrastructure provisioned by hand through a console — click to create a VM, click to open a port — leaves no record of what was done, by whom, or why, and cannot be reliably reproduced in a second environment. Infrastructure as code (IaC) describes infrastructure declaratively in version-controlled files; a tool (Terraform is the example here, not the only one) reads that description and makes the real infrastructure match it. Builds on: → 178 (compute, storage and managed services).

Diagram

Code (desired state, in git) ──► plan (diff vs. real state) ──► apply (make reality match)
                                                                        │
real infrastructure ◄──────────────────────────────────────────────────┘
        │
        └── manual change ──► drift: real state no longer matches the code

How it works

  1. Infrastructure is declared in files — "this VPC, these subnets, this database, this many instances" — committed to version control exactly like application code, so review, history, and rollback apply to infrastructure changes too.
  2. Running plan compares declared state against real infrastructure's current state and computes a diff: what would be created, changed, or destroyed to make reality match the code — nothing changes yet.
  3. Running apply executes that diff against the real provider APIs — the same declarative, reconciliation-style model Kubernetes uses (→ 181), applied to the infrastructure layer instead of the workload layer.
  4. State — the tool's record of what it believes exists and with what configuration — is stored remotely and locked during an apply, so two people running apply concurrently don't corrupt each other's changes or the infrastructure itself.
  5. Modules package a reusable, parameterized chunk of infrastructure so dev, staging, and production are provisioned from the same reviewed definition with only parameters differing — the mechanism behind environment parity (→ 186).
  6. Infrastructure drift is what happens when real infrastructure changes outside the tool — a manual console fix during an incident, a direct provider-API change — leaving the tool's state and reality out of sync; the next plan either reports unexpected changes it would "fix" (reverting the manual change) or, if the manual change wasn't tracked at all, silently misses it.
  7. Detecting drift means periodically running plan against production and reviewing every unexpected diff before deciding whether to codify the manual change (bring it into the files) or revert it (let the tool put reality back).

Practical example

(numbers illustrative). During an incident, an engineer manually raises a database's connection limit directly in the provider console to stop the bleeding — infrastructure now differs from code. Weeks later, an unrelated change runs apply; the plan silently includes "revert connection limit to the old value," because the code was never updated to reflect the manual fix. Without a drift-detection habit, that revert ships in a routine change and the original incident's symptom returns, with no one connecting the two until the postmortem (→ 206).

When to use it

  • Use IaC for anything provisioned more than once, anything that must be reproducible across environments, or anything requiring an audit trail of who changed what and why.
  • Run plan-only drift checks on a schedule, not just before intentional changes — drift accumulates silently between deliberate applies.
  • Avoid manual console changes to anything managed by IaC except as a genuinely temporary incident mitigation — and when you do, codify the change in the files within the same day, before it's forgotten.
  • Failure scenarios: the silent-revert-of-a-manual-fix above; concurrent applies without state locking corrupting the tool's record of reality; a module parameterized so loosely that staging and production drift apart in ways nobody notices until production behaves differently under load.
  • Simpler alternative: for a single, rarely changed resource (one VM, provisioned once), manual provisioning with a written runbook is defensible — IaC earns its overhead once a resource is provisioned more than once, across more than one environment, or by more than one person.

Practice

(process kind, 20–30 min, paper exercise — or hands-on if you have access to any IaC tool and a free-tier account). Write (or sketch, if no tool access) the declarative description for a minimal VPC with one subnet and one compute instance; then describe a manual change you'd make directly against the provider during an incident, predict exactly what the next plan would report, and state whether you'd codify the change or let the tool revert it. Done when the predicted plan output matches what the described drift would actually produce.

Key takeaway

Infrastructure as code makes infrastructure changes reviewable, reproducible, and reversible the same way application code is; drift is what breaks that guarantee, and it only shows up if you look for it on a schedule, not just before planned changes. Done when you can: explain what a drift report would show for a given manual change, and decide whether to codify or revert it. Used next in → 186 (CI/CD runs plan and apply as pipeline steps), → 190 (platform engineering templates IaC modules as paved roads).

References

None — this topic makes no external claims.

Source directory

Search the guide