What if your computer could finish the boring setup work for you every time you change cloud resources?
Why Local-exec provisioner in Terraform? - Purpose & Use Cases
Imagine you create a server in the cloud and then have to manually run commands on your own computer to configure it or move files every time you make a change.
This manual way is slow and easy to forget steps. You might run commands in the wrong order or miss one, causing errors and wasting time.
The Local-exec provisioner lets Terraform automatically run commands on your local machine right after creating or changing resources. This means no more manual steps or mistakes.
terraform apply # Then manually run: scp file.txt user@server:/path # Then ssh user@server 'sudo systemctl restart service'
resource "null_resource" "example" { provisioner "local-exec" { command = "scp file.txt user@server:/path && ssh user@server 'sudo systemctl restart service'" } }
You can automate local tasks triggered by cloud changes, making your deployments faster and error-free.
After launching a new virtual machine, automatically copying configuration files from your laptop and restarting services without lifting a finger.
Manual local commands are slow and error-prone.
Local-exec runs local commands automatically during Terraform runs.
This saves time and reduces mistakes in cloud setups.
Practice
local-exec provisioner do in Terraform?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
Thelocal-execprovisioner specifically runs commands on your local machine, not inside the cloud resource.Final Answer:
Runs commands on your local machine after resource creation -> Option DQuick Check:
local-exec runs commands locally [OK]
- Confusing local-exec with remote-exec
- Thinking it runs inside the cloud resource
- Assuming it manages Terraform state files
local-exec provisioner in a Terraform resource?Solution
Step 1: Recall Terraform provisioner syntax
Provisioners use the keywordprovisionerfollowed by the provisioner type in quotes.Step 2: Check correct attribute names
Thelocal-execprovisioner usescommandto specify the shell command to run.Final Answer:
provisioner "local-exec" { command = "echo Hello" } -> Option AQuick Check:
Correct syntax uses provisioner "local-exec" and command [OK]
- Omitting quotes around local-exec
- Using wrong attribute names like run or execute
- Using underscore instead of dash in local-exec
resource "null_resource" "example" {
provisioner "local-exec" {
command = "echo Resource created"
}
}Solution
Step 1: Understand null_resource and local-exec
Thenull_resourceis a Terraform resource that supports provisioners for running commands.Step 2: Analyze the command execution
Thelocal-execprovisioner runs the command on your local machine, so it will print the message locally.Final Answer:
Terraform will print 'Resource created' on your local machine -> Option CQuick Check:
local-exec runs commands locally, so output appears locally [OK]
- Thinking output appears inside cloud resource
- Believing null_resource disallows provisioners
- Assuming provisioners are ignored silently
resource "null_resource" "fail_example" {
provisioner "local-exec" {
command = echo "Hello"
}
}
What is the cause of the error?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 AQuick Check:
Command must be a quoted string [OK]
- Forgetting quotes around command
- Using wrong attribute names
- Assuming null_resource disallows provisioners
setup.sh after creating an AWS EC2 instance using Terraform. Which is the best way to use local-exec provisioner to achieve this safely?Solution
Step 1: Understand local-exec vs remote-exec
Thelocal-execprovisioner 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 theremote-execprovisioner which executes commands on the remote resource.Final Answer:
Useremote-execprovisioner to runsetup.shon the EC2 instance -> Option BQuick Check:
remote-exec runs commands on the remote resource [OK]
- Using remote-exec for a local script
- Running scripts manually instead of automating
- Adding unnecessary depends_on inside the resource
Ready to prove it?
Pass the test and this pattern moves unknown
