Check blocks for assertions in Terraform - Time & Space Complexity
We want to understand how the time to run Terraform changes when using check blocks with assertions.
Specifically, how does the number of checks affect the total execution time?
Analyze the time complexity of this Terraform check block with assertions.
check "assertions" {
assertions = [
for resource in var.resources : {
condition = resource.enabled
error_message = "Resource ${resource.name} must be enabled"
}
]
}
This block checks each resource in a list to ensure it is enabled, raising an error if not.
We look for repeated actions in this check block.
- Primary operation: Evaluating the condition for each resource in the list.
- How many times: Once per resource, so as many times as there are resources.
As the number of resources grows, the number of condition checks grows the same way.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 condition checks |
| 100 | 100 condition checks |
| 1000 | 1000 condition checks |
Pattern observation: The number of checks grows directly with the number of resources.
Time Complexity: O(n)
This means the time to run the check grows linearly with the number of resources checked.
[X] Wrong: "The check block runs only once no matter how many resources there are."
[OK] Correct: Each resource's condition is evaluated separately, so more resources mean more checks.
Understanding how checks scale helps you design efficient Terraform validations and predict deployment times.
"What if we added nested loops inside the check block to validate pairs of resources? How would the time complexity change?"