Provisioner failure behavior in Terraform - Time & Space 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.
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 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.
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 |
|---|---|
| 10 | 10 provisioner runs |
| 100 | 100 provisioner runs |
| 1000 | 1000 provisioner runs |
Pattern observation: The total execution time increases linearly as the number of provisioners increases.
Time Complexity: O(n)
This means the time to run all provisioners grows directly with how many provisioners you have.
[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.
Knowing how provisioner failures affect deployment time helps you design reliable and predictable infrastructure automation.
"What if we changed on_failure from 'continue' to 'fail'? How would the time complexity change when a provisioner fails early?"
Practice
What does the on_failure argument in a Terraform provisioner control?
Solution
Step 1: Understand provisioner purpose
Provisioners run scripts or commands after resource creation.Step 2: Role of
Theon_failureon_failureargument controls what Terraform does if the provisioner fails.Final Answer:
It controls what happens if the provisioner script or command fails. -> Option AQuick Check:
on_failurecontrols failure behavior [OK]
- Confusing on_failure with timeout settings
- Thinking on_failure sets provisioner order
- Assuming on_failure defines OS type
Which of the following is the correct syntax to make a provisioner continue even if it fails?
provisioner "local-exec" {
command = "echo Hello"
on_failure = ?
}Solution
Step 1: Recall valid
Terraform supportson_failurevaluesfailandcontinueas validon_failurevalues.Step 2: Identify correct value to continue on failure
To continue even if the provisioner fails, usecontinue.Final Answer:
"continue" -> Option AQuick Check:
on_failure = "continue"continues on failure [OK]
- Using invalid values like "ignore" or "skip"
- Confusing "fail" with continuing on failure
- Omitting quotes around the value
Given this Terraform snippet, what happens if the local-exec provisioner command fails?
resource "null_resource" "example" {
provisioner "local-exec" {
command = "exit 1"
on_failure = "fail"
}
}Solution
Step 1: Analyze provisioner command and on_failure
The command "exit 1" will fail. Theon_failureis set to "fail".Step 2: Understand effect of
This setting causes Terraform to stop and report an error if the provisioner fails.on_failure = "fail"Final Answer:
Terraform stops and reports an error, halting the apply process. -> Option CQuick Check:
on_failure = "fail"stops on failure [OK]
- Assuming Terraform retries automatically
- Thinking failure is ignored with "fail"
- Believing resource is created despite failure
Identify the error in this provisioner block that causes Terraform to ignore failures unexpectedly:
provisioner "remote-exec" {
inline = ["false_command"]
on_failure = fail
}Solution
Step 1: Check
The value foron_failuresyntaxon_failuremust be a quoted string, e.g., "fail" or "continue".Step 2: Identify impact of missing quotes
Without quotes, Terraform treats it as an identifier, causing unexpected behavior or ignoring the setting.Final Answer:
Theon_failurevalue should be a string with quotes. -> Option BQuick Check:
on_failurevalues require quotes [OK]
on_failure values "fail" or "continue" [OK]- Omitting quotes around
on_failurevalues - Confusing command errors with syntax errors
- Thinking
on_failureis unsupported
You want to run a local-exec provisioner that might fail but should not stop Terraform from creating other resources. Which configuration achieves this?
resource "null_resource" "example" {
provisioner "local-exec" {
command = "some_command"
on_failure = ?
}
}Solution
Step 1: Understand requirement to not stop Terraform
The provisioner may fail but Terraform should continue creating other resources.Step 2: Choose correct
Usingon_failurevalueon_failure = "continue"tells Terraform to ignore provisioner failure and proceed.Final Answer:
"continue" to ignore failure and proceed -> Option DQuick Check:
on_failure = "continue"allows continuation on failure [OK]
- Using unsupported values like "retry" or "ignore"
- Choosing "fail" which stops Terraform
- Not quoting the
on_failurevalue
Ready to prove it?
Pass the test and this pattern moves unknown
