What if your cloud setup silently changes without your code knowing?
Why Drift detection in CI/CD in Terraform? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you manage a cloud setup by manually changing settings here and there. You update a server, tweak a database, or add a new network rule directly in the cloud console. But you forget to update your code or scripts that describe this setup.
Later, when you run your automation to update infrastructure, it doesn't know about those manual changes. This causes confusion and unexpected results.
Manually tracking every change is slow and tiring. It's easy to forget what was changed or where. This leads to errors, security risks, and downtime because your automated setup and actual cloud state don't match.
Fixing these mismatches takes time and can break your deployment process.
Drift detection in CI/CD automatically checks if your real infrastructure matches your code before making changes. It spots differences early, so you can fix them or update your code.
This keeps your setup reliable and your automation trustworthy, saving time and avoiding surprises.
terraform apply
# But manual changes outside terraform cause issuesterraform plan
# Detects drift before applying changesIt enables smooth, safe updates by ensuring your code and infrastructure always stay in sync.
A team uses Terraform to manage cloud servers. Someone manually adds a firewall rule in the cloud console. Drift detection spots this difference during CI/CD, alerting the team to update their Terraform code before deployment.
Manual changes cause hidden mismatches and risks.
Drift detection finds differences automatically before deployment.
This keeps infrastructure and code aligned, making updates safer and faster.
Practice
Solution
Step 1: Understand drift detection concept
Drift detection compares the current real infrastructure state with the Terraform code to find differences.Step 2: Identify the purpose in CI/CD
In CI/CD, drift detection helps catch unexpected changes before applying new updates.Final Answer:
To find differences between the Terraform code and the actual infrastructure -> Option AQuick Check:
Drift detection = find differences [OK]
- Thinking drift detection speeds deployment
- Assuming it deletes resources automatically
- Confusing it with documentation generation
Solution
Step 1: Recall Terraform commands
terraform plan shows the changes Terraform will make without applying them.Step 2: Identify drift detection command
terraform plan detects differences (drift) between code and real infrastructure before apply.Final Answer:
terraform plan -> Option AQuick Check:
Detect drift = terraform plan [OK]
- Using terraform apply which changes infrastructure
- Confusing terraform init with drift detection
- Using terraform destroy which deletes resources
# aws_instance.example will be updated in-place
~ tags = {
- "Environment" = "dev"
+ "Environment" = "prod"
}What does this output indicate about drift?
Solution
Step 1: Analyze the plan output
The '~' symbol means in-place update. The tag "Environment" changes from "dev" to "prod".Step 2: Understand drift implication
This shows drift: the real infrastructure tag differs from code and will be updated.Final Answer:
The tag "Environment" has drifted from "dev" to "prod" and will be updated -> Option DQuick Check:
~ means update tag from dev to prod [OK]
- Thinking resource will be destroyed instead of updated
- Ignoring tag changes as no drift
- Assuming Terraform ignores tag differences
terraform plan in your CI/CD pipeline but it does not detect drift even though manual changes were made outside Terraform. What is the most likely cause?Solution
Step 1: Understand drift detection dependency
Terraform relies on the state file to compare real infrastructure with code.Step 2: Identify cause of missed drift
If the state file is outdated or corrupted, Terraform cannot detect manual changes (drift).Final Answer:
Terraform state file is outdated or corrupted -> Option CQuick Check:
State file outdated = missed drift detection [OK]
- Assuming terraform apply affects drift detection
- Believing Terraform ignores manual changes by design
- Thinking missing provider causes drift detection failure
Solution
Step 1: Understand CI/CD drift detection goal
The goal is to detect drift and fail early before applying changes.Step 2: Choose correct command and method
terraform plan shows drift without applying; parsing its output allows pipeline to fail if drift exists.Step 3: Evaluate other options
Applying directly risks unwanted changes; manual checks are slow; skipping detection is unsafe.Final Answer:
Run terraform plan and parse its output to detect changes, then fail if changes exist -> Option BQuick Check:
Plan + parse output = fail on drift [OK]
- Applying without checking drift first
- Relying on manual checks in automated pipelines
- Ignoring drift detection to speed up deploys
