0
0
Terraformcloud~5 mins

Check blocks for assertions in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Check blocks for assertions
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of resources grows, the number of condition checks grows the same way.

Input Size (n)Approx. Api Calls/Operations
1010 condition checks
100100 condition checks
10001000 condition checks

Pattern observation: The number of checks grows directly with the number of resources.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the check grows linearly with the number of resources checked.

Common Mistake

[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.

Interview Connect

Understanding how checks scale helps you design efficient Terraform validations and predict deployment times.

Self-Check

"What if we added nested loops inside the check block to validate pairs of resources? How would the time complexity change?"