0
0
Terraformcloud~10 mins

Provisioner failure behavior in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Provisioner failure behavior
Start Terraform Apply
Create Resource
Run Provisioner
Provisioner Success?
NoHandle Failure
Fail or Continue
Resource Ready
End
Terraform creates a resource, runs the provisioner, then checks if it succeeded. On failure, it either stops or continues based on settings.
Execution Sample
Terraform
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  provisioner "local-exec" {
    command = "exit 1"
  }
}
This Terraform code creates an AWS instance and runs a local-exec provisioner that fails (exit 1).
Process Table
StepActionProvisioner ResultBehavior
1Start terraform applyN/ABegin resource creation
2Create aws_instance.exampleN/AResource creation started
3Run local-exec provisionerFailure (exit 1)Provisioner failed
4Check failure handlingFailure detectedTerraform stops apply by default
5Apply terminatesN/AResource creation rolled back
💡 Provisioner failed and Terraform stopped apply, rolling back resource creation
Status Tracker
VariableStartAfter Step 2After Step 3Final
Resource StateNot createdCreatingProvisioner failedRolled back
Provisioner StatusNot runNot runFailedFailed
Key Moments - 2 Insights
Why does Terraform stop the apply when the provisioner fails?
By default, Terraform treats provisioner failure as a critical error and stops the apply to avoid inconsistent state, as shown in step 4 of the execution_table.
Can Terraform continue if a provisioner fails?
Yes, if you set 'on_failure = "continue"' in the provisioner block, Terraform will continue despite failure. This is not shown in the sample but changes behavior after step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the provisioner result at step 3?
AFailure (exit 1)
BSuccess
CNot run
DSkipped
💡 Hint
Check the 'Provisioner Result' column at step 3 in the execution_table.
At which step does Terraform decide to stop the apply due to provisioner failure?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Behavior' column to see when Terraform stops, in step 4.
If 'on_failure = continue' was set, how would the behavior at step 4 change?
ATerraform would stop apply immediately
BProvisioner would not run
CTerraform would continue apply despite failure
DResource creation would be skipped
💡 Hint
Refer to key_moments about continuing on provisioner failure.
Concept Snapshot
Terraform runs provisioners after resource creation.
If a provisioner fails, Terraform stops apply by default.
Use 'on_failure = continue' to continue despite failure.
Failure causes resource rollback to keep state consistent.
Full Transcript
Terraform applies start by creating resources. After creation, provisioners run to perform extra setup. If a provisioner fails, Terraform detects this failure and stops the apply process to avoid inconsistent infrastructure. This means the resource creation is rolled back. However, you can configure provisioners with 'on_failure = continue' to let Terraform continue even if the provisioner fails. This behavior ensures your infrastructure state remains reliable and predictable.