0
0
Terraformcloud~10 mins

Why provisioners run scripts on resources in Terraform - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why provisioners run scripts on resources
Terraform creates resource
Resource is ready
Provisioner runs script
Script configures resource
Resource fully configured
End
Terraform first creates the resource, then runs scripts on it using provisioners to configure or customize it after creation.
Execution Sample
Terraform
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  connection {
    type        = "ssh"
    user        = "ubuntu"
    host        = self.public_ip
    private_key = file("private_key.pem")
  }

  provisioner "remote-exec" {
    inline = ["echo Hello > /tmp/hello.txt"]
  }
}
This Terraform code creates an AWS instance and runs a script on it to create a file after the instance is ready.
Process Table
StepActionResource StateProvisioner Script RunResult
1Start Terraform applyNo resourceNoWaiting to create resource
2Create AWS instanceInstance launchingNoInstance is being created
3Instance readyInstance runningYesProvisioner script runs
4Run script: echo Hello > /tmp/hello.txtInstance runningYesFile /tmp/hello.txt created
5Provisioner completesInstance runningNoResource fully configured
6Terraform apply endsInstance runningNoApply complete
💡 Terraform stops after resource is created and provisioner script runs successfully
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Resource StateNoneLaunchingRunningRunningRunning
Provisioner Script RunNoNoYesYesNo
Key Moments - 3 Insights
Why does Terraform run scripts only after the resource is created?
Terraform waits until the resource is ready (see Step 3 in execution_table) because scripts need the resource to exist to configure it.
What happens if the provisioner script fails?
Terraform will stop the apply process at the provisioner step (Step 4) and report an error, preventing incomplete configuration.
Can provisioners run scripts before resource creation?
No, provisioners run only after resource creation (Step 3) because they need to act on the existing resource.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the provisioner script start running?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Provisioner Script Run' column in execution_table rows
According to variable_tracker, what is the resource state after Step 4?
ANone
BLaunching
CRunning
DTerminated
💡 Hint
Look at 'Resource State' row after Step 4 in variable_tracker
If the provisioner script fails at Step 4, what happens to the Terraform apply process?
ATerraform retries the resource creation
BTerraform stops and reports an error
CTerraform continues to Step 5
DTerraform ignores the failure and finishes
💡 Hint
Refer to key_moments about provisioner failure explanation
Concept Snapshot
Terraform creates resources first.
Provisioners run scripts after resource creation.
Scripts configure or customize the resource.
If scripts fail, Terraform stops apply.
Provisioners cannot run before resource exists.
Full Transcript
Terraform first creates the resource, such as a virtual machine. Once the resource is ready and running, Terraform runs provisioner scripts on it. These scripts can configure the resource, like creating files or installing software. Provisioners run only after the resource exists because they need something to act on. If a provisioner script fails, Terraform stops the apply process and reports an error to avoid incomplete setups. This ensures resources are fully configured as intended.