Course contentsShow
← Cloud and Infrastructure
Lesson 705 of 2,98913. Infrastructure as Code FoundationsFree lesson

State Refresh and Drift Detection

Understand how IaC tools refresh state from real infrastructure to detect manual changes.

State Refresh and Drift Detection

What you'll learn: How IaC tools detect when someone makes manual changes to your infrastructure outside of code.

The Problem: Reality Drifts from Your Code

You've defined your infrastructure in code and applied it—everything matches perfectly. But then someone logs into the cloud console and manually resizes a server, changes a security rule, or deletes a resource. Your infrastructure has now drifted from what your IaC code describes.

How State Refresh Works

Before making any changes, IaC tools perform a state refresh—they query your actual cloud resources and compare them to what's recorded in the state file. This is like taking inventory: "Does the real infrastructure still match what I think I deployed?"

During refresh, the tool:

  1. Reads the current state file (your last known configuration)
  2. Queries the cloud provider APIs for current resource details
  3. Compares reality against expectations
  4. Updates the state file with current values
  5. Reports any differences (drift)

Detecting Drift

Drift means your real infrastructure no longer matches your IaC definition. Maybe a resource was deleted, a property changed, or something was added manually. The refresh process exposes this mismatch, allowing you to decide: should you update your code to match reality, or re-apply your code to fix the manual change?

Think of it like a recipe: if someone added extra salt to your dish after you cooked it, you need to know about it before cooking the next batch the same way.

Key Takeaway: State refresh queries real infrastructure and compares it against your state file to detect drift—manual changes made outside your IaC workflow—so you can maintain consistency and control.