Drift detection in CI/CD in Terraform - Time & Space Complexity
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?"