Detecting Configuration Drift
What you'll learn: How to use Terraform to identify when your real-world infrastructure no longer matches what Terraform expects.
What is Configuration Drift?
Configuration drift happens when someone (or something) changes your infrastructure outside of Terraform's control. Maybe a colleague tweaked a security group in the AWS console, or an automated script modified a server setting, or a developer manually changed a database parameter. Whatever the cause, your actual infrastructure now differs from what's recorded in your Terraform state file.
How Terraform Detects Drift
When you run terraform plan or terraform refresh, Terraform queries your cloud provider's API to check the current state of your resources. It then compares what it finds against what's stored in your state file.
If differences exist, Terraform flags them as drift. The plan output will show:
- Resources that have changed attributes
- Resources that were deleted manually
- Resources that were created outside Terraform
Think of it like a home security system: Terraform regularly checks whether everything is where it should be. If a window that should be closed is now open, the system alerts you.
Why This Matters
Detecting drift is crucial because:
- It prevents surprises during the next
terraform apply - It helps you decide whether to update your code or revert manual changes
- It maintains the "single source of truth" principle of IaC
Key Takeaway: Terraform detects configuration drift by comparing your state file against the real infrastructure during plan and refresh operations, alerting you when manual changes have occurred outside Terraform's control.