Bird
Raised Fist0
Terraformcloud~10 mins

Why provisioners run scripts on resources in Terraform - Visual Breakdown

Choose your learning style10 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.

Practice

(1/5)
1. Why do Terraform provisioners run scripts on resources?
easy
A. To backup resource data before creation
B. To delete resources automatically
C. To configure resources after they are created
D. To monitor resource usage continuously

Solution

  1. Step 1: Understand provisioners' purpose

    Provisioners run scripts to configure resources after Terraform creates them.
  2. Step 2: Identify correct use case

    They help install software or run commands Terraform cannot manage directly.
  3. Final Answer:

    To configure resources after they are created -> Option C
  4. Quick Check:

    Provisioners configure resources post-creation = D [OK]
Hint: Provisioners run scripts after resource creation [OK]
Common Mistakes:
  • Thinking provisioners delete resources
  • Assuming provisioners monitor resources
  • Believing provisioners backup data
2. Which of the following is the correct syntax to use a local-exec provisioner in Terraform?
easy
A. provisioner "local-exec" { command = "echo Hello" }
B. provisioner local_exec { run = "echo Hello" }
C. provisioner "local-exec" (command: "echo Hello")
D. provisioner local-exec { execute = "echo Hello" }

Solution

  1. Step 1: Recall correct provisioner block syntax

    Terraform uses provisioner blocks with type in quotes and a command attribute.
  2. Step 2: Match syntax to options

    provisioner "local-exec" { command = "echo Hello" } matches the correct syntax: provisioner "local-exec" { command = "echo Hello" }.
  3. Final Answer:

    provisioner "local-exec" { command = "echo Hello" } -> Option A
  4. Quick Check:

    Correct provisioner syntax uses quotes and command attribute = A [OK]
Hint: Use quotes and command attribute for local-exec provisioner [OK]
Common Mistakes:
  • Omitting quotes around provisioner type
  • Using wrong attribute names like run or execute
  • Using parentheses instead of braces
3. Given this Terraform snippet, what will happen when the resource is created?
resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "echo Resource created"
  }
}
medium
A. Terraform will fail because null_resource cannot have provisioners
B. Terraform will print 'Resource created' after creating the resource
C. Terraform will create the resource but not run the command
D. Terraform will run the command before creating the resource

Solution

  1. Step 1: Understand null_resource and local-exec provisioner

    null_resource supports provisioners; local-exec runs commands on the machine running Terraform after resource creation.
  2. Step 2: Analyze command execution timing

    The command 'echo Resource created' runs after the resource is created, printing the message.
  3. Final Answer:

    Terraform will print 'Resource created' after creating the resource -> Option B
  4. Quick Check:

    local-exec runs command post-creation = A [OK]
Hint: local-exec runs commands after resource creation [OK]
Common Mistakes:
  • Thinking null_resource disallows provisioners
  • Assuming commands run before resource creation
  • Believing commands won't run at all
4. You wrote this provisioner block but Terraform shows an error:
provisioner "remote-exec" {
  command = "sudo apt-get update"
}
What is the likely cause of the error?
medium
A. Provisioners cannot run sudo commands
B. The command attribute is invalid for remote-exec
C. remote-exec provisioner requires a local script file
D. Missing connection block to specify how to connect to the resource

Solution

  1. Step 1: Check remote-exec requirements

    remote-exec provisioner needs a connection block to connect to the resource (SSH or WinRM).
  2. Step 2: Identify missing connection block

    The snippet lacks a connection block, causing Terraform to error because it cannot connect to run the command.
  3. Final Answer:

    Missing connection block to specify how to connect to the resource -> Option D
  4. Quick Check:

    remote-exec needs connection block = C [OK]
Hint: Always add connection block for remote-exec provisioners [OK]
Common Mistakes:
  • Assuming command attribute is invalid
  • Thinking sudo commands are disallowed
  • Believing remote-exec runs local scripts only
5. You want to install software on a new VM after Terraform creates it. Which approach best uses provisioners to do this reliably?
hard
A. Use a remote-exec provisioner with a connection block to run installation commands after creation
B. Use a local-exec provisioner to run installation commands on your local machine
C. Run installation commands manually after Terraform finishes
D. Use a provisioner without connection block to run commands remotely

Solution

  1. Step 1: Identify provisioner type for remote VM configuration

    remote-exec provisioner runs commands on the remote VM after creation using a connection block.
  2. Step 2: Evaluate options for reliability

    Using remote-exec with connection block automates installation reliably; local-exec runs locally, manual runs are error-prone, and missing connection causes failure.
  3. Final Answer:

    Use a remote-exec provisioner with a connection block to run installation commands after creation -> Option A
  4. Quick Check:

    remote-exec + connection block = reliable remote config [OK]
Hint: Use remote-exec with connection block for remote installs [OK]
Common Mistakes:
  • Using local-exec for remote installs
  • Skipping connection block for remote-exec
  • Relying on manual post-creation steps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard