What is the purpose of the local-exec provisioner in Terraform?
The local-exec provisioner runs a command on the machine where Terraform is executed, allowing you to perform local actions during resource creation or destruction.
Click to reveal answer
beginner
How does local-exec differ from remote-exec provisioner?
local-exec runs commands on the local machine where Terraform runs, while remote-exec runs commands on the remote resource (like a VM) after it is created.
Click to reveal answer
beginner
Which argument is required inside a local-exec provisioner block to specify the command to run?
The command argument specifies the shell command or script to execute locally.
Click to reveal answer
intermediate
Can local-exec provisioner access Terraform resource attributes?
Yes, you can use Terraform interpolation syntax to pass resource attributes as part of the command string to local-exec.
Click to reveal answer
beginner
What is a common use case for local-exec provisioner?
A common use case is to run local scripts for configuration, notify external systems, or trigger local automation after resource creation.
Click to reveal answer
Where does the local-exec provisioner run its commands?
AOn the remote resource created
BOn the machine running Terraform
COn the Terraform cloud server
DOn a separate management server
✗ Incorrect
The local-exec provisioner runs commands on the local machine where Terraform is executed.
Which argument must you include in a local-exec provisioner block?
Ainline
Bscript_path
Ccommand
Dremote_command
✗ Incorrect
The command argument specifies the command to run locally.
Can local-exec provisioner run after a resource is destroyed?
ANo, it only runs on creation
BOnly on resource update
COnly if manually triggered
DYes, using the <code>when = destroy</code> setting
✗ Incorrect
You can configure local-exec to run during resource destruction by setting when = destroy.
Which of these is NOT a recommended use of local-exec?
ARunning commands on the remote VM
BTriggering local notifications
CRunning local scripts after resource creation
DCalling local automation tools
✗ Incorrect
Running commands on the remote VM should use remote-exec, not local-exec.
How can you pass resource data to a local-exec command?
AUsing Terraform interpolation syntax inside the command string
BUsing environment variables only
CYou cannot pass resource data
DBy writing to a remote file first
✗ Incorrect
Terraform interpolation allows embedding resource attributes directly in the command string.
Explain what the local-exec provisioner does in Terraform and give an example of when you might use it.
Think about actions you want to perform on your own computer after creating cloud resources.
You got /3 concepts.
Describe how you would configure a local-exec provisioner to run a shell command that uses an attribute from a created resource.
Remember Terraform lets you insert resource data inside strings with ${}.
You got /3 concepts.
Practice
(1/5)
1. What does the local-exec provisioner do in Terraform?
easy
A. Runs commands inside the cloud resource
B. Executes Terraform plan without applying changes
C. Creates a local copy of the Terraform state file
D. Runs commands on your local machine after resource creation
Solution
Step 1: Understand provisioner types
Terraform provisioners run scripts or commands either locally or remotely after resource creation.
Step 2: Identify local-exec purpose
The local-exec provisioner specifically runs commands on your local machine, not inside the cloud resource.
Final Answer:
Runs commands on your local machine after resource creation -> Option D
Quick Check:
local-exec runs commands locally [OK]
Hint: Local-exec runs commands on your own computer [OK]
Common Mistakes:
Confusing local-exec with remote-exec
Thinking it runs inside the cloud resource
Assuming it manages Terraform state files
2. Which of the following is the correct syntax to use a local-exec provisioner in a Terraform resource?
easy
A. provisioner "local-exec" { command = "echo Hello" }
B. provisioner local-exec { run = "echo Hello" }
C. local_exec { command = "echo Hello" }
D. provisioner "local_exec" { execute = "echo Hello" }
Solution
Step 1: Recall Terraform provisioner syntax
Provisioners use the keyword provisioner followed by the provisioner type in quotes.
Step 2: Check correct attribute names
The local-exec provisioner uses command to specify the shell command to run.
B. local-exec provisioner cannot be used with null_resource
C. The command attribute should be named run
D. Terraform does not support echo commands
Solution
Step 1: Check command syntax
The command value must be a string enclosed in quotes in Terraform configuration.
Step 2: Identify missing quotes
Here, command = echo "Hello" lacks quotes around the entire command string, causing a syntax error.
Final Answer:
Missing quotes around the command string -> Option A
Quick Check:
Command must be a quoted string [OK]
Hint: Always quote command strings in local-exec [OK]
Common Mistakes:
Forgetting quotes around command
Using wrong attribute names
Assuming null_resource disallows provisioners
5. You want to run a local script setup.sh after creating an AWS EC2 instance using Terraform. Which is the best way to use local-exec provisioner to achieve this safely?
hard
A. Run setup.sh manually after Terraform apply
B. Use remote-exec provisioner to run setup.sh on the EC2 instance
C. Use local-exec with command = "./setup.sh" inside the EC2 resource without dependencies
D. Use local-exec with command = "./setup.sh" inside the EC2 resource and add depends_on for the instance
Solution
Step 1: Understand local-exec vs remote-exec
The local-exec provisioner runs commands on your local machine, which is not suitable for running scripts on the EC2 instance itself.
Step 2: Use remote-exec for remote commands
To run a script on the EC2 instance, use the remote-exec provisioner which executes commands on the remote resource.
Final Answer:
Use remote-exec provisioner to run setup.sh on the EC2 instance -> Option B
Quick Check:
remote-exec runs commands on the remote resource [OK]
Hint: Use remote-exec to run scripts on remote instances [OK]
Common Mistakes:
Using remote-exec for a local script
Running scripts manually instead of automating
Adding unnecessary depends_on inside the resource
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.