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:
- Reads the current state file (your last known configuration)
- Queries the cloud provider APIs for current resource details
- Compares reality against expectations
- Updates the state file with current values
- 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.