Course contentsShow
← Security
Lesson 2024 of 3,05240. Cloud Security - Compliance and GovernanceFree lesson

Drift Detection in Terraform and IaC Tools

Leveraging terraform plan, drift detection commands, and state file comparisons to identify infrastructure changes.

Drift Detection in Terraform and IaC Tools

You'll learn how to identify unauthorized infrastructure changes by comparing your IaC definitions against actual cloud resources.

What is Drift Detection in Practice?

Think of your Terraform code as a blueprint and your cloud infrastructure as the built house. Drift detection is like a building inspector comparing what was built against the original plans to spot unauthorized modifications—maybe someone added a door or removed a wall without updating the blueprints.

In cloud security, drift occurs when resources are modified outside your IaC workflow (manual console changes, direct API calls, or external automation). These undocumented changes create security blind spots and compliance violations.

Core Detection Mechanisms

terraform plan is your first line of defense. Run it regularly against your deployed infrastructure—if it shows any proposed changes when you haven't modified your code, you've found drift. The output reveals exactly what differs between your desired state (code) and actual state (cloud).

State file comparison is the foundation. Terraform stores a snapshot of your infrastructure in its state file. When you run commands, Terraform queries the real cloud resources and compares them to this state. Mismatches indicate drift.

Drift detection commands in modern IaC tools automate this:

  • terraform plan -refresh-only updates state to match reality without making changes
  • Many CI/CD pipelines run scheduled drift detection jobs
  • Tools like Terraform Cloud offer automatic drift detection with notifications

Why This Matters for Security

Undetected drift can introduce misconfigurations, expose secrets, or bypass security controls you thought were enforced. Regular drift detection ensures your security posture matches your documented, reviewed, and approved infrastructure definitions.

Key Takeaway: Use terraform plan, state comparisons, and automated drift detection workflows to continuously verify that your deployed infrastructure matches your IaC definitions, catching unauthorized security-impacting changes before they become incidents.