0
0
Terraformcloud~10 mins

Local-exec provisioner in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Local-exec provisioner
Terraform plan starts
Resource created
Local-exec provisioner triggers
Runs local command/script
Command success?
NoError stops apply
Yes
Terraform apply continues/completes
Terraform creates a resource, then runs a local command on your machine using local-exec provisioner. If the command fails, apply stops.
Execution Sample
Terraform
resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "echo Hello from local-exec"
  }
}
Creates a dummy resource and runs a local shell command printing a message on your machine.
Process Table
StepActionResource StateProvisioner CommandCommand ResultNext Step
1Start terraform applyNo resourceN/AN/ACreate resource
2Create null_resource.exampleResource createdN/AN/ARun local-exec provisioner
3Run local-exec commandResource createdecho Hello from local-execHello from local-exec printedCheck command success
4Check command successResource createdN/ASuccessComplete apply
5Terraform apply completesResource createdN/AN/AEnd
💡 Local-exec command succeeded, so terraform apply completes successfully.
Status Tracker
VariableStartAfter Step 2After Step 3Final
Resource StateNo resourceResource createdResource createdResource created
Provisioner CommandN/AN/Aecho Hello from local-execN/A
Command ResultN/AN/AHello from local-exec printedSuccess
Key Moments - 3 Insights
Why does terraform stop if the local-exec command fails?
Terraform treats the local-exec provisioner as part of resource creation. If the command fails (see step 4 in execution_table), terraform stops to avoid inconsistent state.
Does the local-exec command run on the cloud resource?
No, local-exec runs on your local machine where terraform runs, not on the cloud resource itself (step 3 in execution_table).
Can local-exec provisioner access resource attributes?
Yes, you can use resource attributes in the command string, but the command still runs locally (noted in step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the resource state after step 2?
AResource created
BNo resource
CResource deleted
DResource failed
💡 Hint
Check the 'Resource State' column at step 2 in execution_table.
At which step does the local-exec command run?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'Run local-exec command' in the 'Action' column in execution_table.
If the local-exec command fails, what happens to terraform apply?
AIt retries the command infinitely
BIt stops with an error
CIt continues normally
DIt ignores the failure and completes
💡 Hint
Refer to the concept_flow and key_moments about command failure stopping apply.
Concept Snapshot
Local-exec provisioner runs a command on your local machine after resource creation.
Syntax: provisioner "local-exec" { command = "your-command" }
Runs during terraform apply.
If command fails, terraform apply stops.
Useful for local scripts or notifications.
Full Transcript
The local-exec provisioner in terraform runs a command on your local machine after a resource is created. The flow starts with terraform creating the resource, then triggering the local-exec provisioner to run the specified command. If the command succeeds, terraform continues and completes the apply. If it fails, terraform stops to avoid inconsistent state. The command runs locally, not on the cloud resource. You can use resource attributes in the command string. This provisioner is useful for running local scripts or notifications during terraform apply.