Drift detection in CI/CD in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to detect drift grows as the number of resources increases in a Terraform CI/CD pipeline.
How does the cost of checking for drift change when more infrastructure is managed?
Analyze the time complexity of the following Terraform drift detection step in CI/CD.
terraform init
terraform plan -detailed-exitcode
# Exit code 2 means drift detected
# Exit code 0 means no drift
# Exit code 1 means error
This snippet initializes Terraform and runs a plan to detect if the current infrastructure state differs from the desired configuration.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Terraform compares each managed resource's current state with the desired state.
- How many times: Once per resource in the infrastructure.
As the number of resources grows, Terraform must check each one for drift, so the work grows with the number of resources.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 resource checks |
| 100 | 100 resource checks |
| 1000 | 1000 resource checks |
Pattern observation: The number of checks grows directly with the number of resources.
Time Complexity: O(n)
This means the time to detect drift grows linearly as you add more resources to manage.
[X] Wrong: "Drift detection time stays the same no matter how many resources there are."
[OK] Correct: Each resource must be checked, so more resources mean more work and longer detection time.
Understanding how drift detection scales helps you design efficient CI/CD pipelines that stay fast as infrastructure grows.
"What if Terraform cached some resource states locally? How would that affect the time complexity of drift detection?"
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
