0
0
Terraformcloud~5 mins

Provisioner failure behavior in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Provisioner failure behavior
O(n)
Understanding Time Complexity

When Terraform runs provisioners, it may retry or stop on failure. Understanding how the time taken grows with the number of provisioners helps us plan deployments better.

We want to know how the total execution time changes as we add more provisioners that might fail.

Scenario Under Consideration

Analyze the time complexity of running multiple local-exec provisioners with failure handling.

resource "null_resource" "example" {
  count = var.provisioner_count

  provisioner "local-exec" {
    command = "echo Running provisioner ${count.index}"
    on_failure = "continue"
  }
}

This creates multiple resources each with a local-exec provisioner that continues even if it fails.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Running each local-exec provisioner command.
  • How many times: Once per provisioner, equal to the number of provisioners defined.
How Execution Grows With Input

Each provisioner runs its command independently. As you add more provisioners, the total time grows roughly in direct proportion.

Input Size (n)Approx. API Calls/Operations
1010 provisioner runs
100100 provisioner runs
10001000 provisioner runs

Pattern observation: The total execution time increases linearly as the number of provisioners increases.

Final Time Complexity

Time Complexity: O(n)

This means the time to run all provisioners grows directly with how many provisioners you have.

Common Mistake

[X] Wrong: "Provisioners run in parallel, so adding more won't increase total time much."

[OK] Correct: By default, provisioners run one after another, so adding more means more total time.

Interview Connect

Knowing how provisioner failures affect deployment time helps you design reliable and predictable infrastructure automation.

Self-Check

"What if we changed on_failure from 'continue' to 'fail'? How would the time complexity change when a provisioner fails early?"