📖 Scenario: You are setting up a simple cloud server using Terraform. After the server is created, you want to run a local script on your machine to notify you that the server is ready.
🎯 Goal: Build a Terraform configuration that creates a resource and uses the local-exec provisioner to run a local command after resource creation.
📋 What You'll Learn
Create a Terraform resource block for a null resource named example
Add a local-exec provisioner inside the resource
Configure the provisioner to run the exact command echo Server is ready
Ensure the Terraform configuration is valid and deployable
💡 Why This Matters
🌍 Real World
Local-exec provisioners are useful for running scripts or commands on your local machine after cloud resources are created, such as sending notifications or updating local files.
💼 Career
Understanding provisioners helps cloud engineers automate deployment workflows and integrate Terraform with other tools and scripts.
Progress0 / 4 steps
1
Create a null resource named example
Write a Terraform resource block for a null_resource named example with an empty body.
Terraform
Hint
The null_resource is a special Terraform resource that does nothing but can be used with provisioners.
2
Add a local-exec provisioner block
Inside the null_resource "example" block, add a provisioner "local-exec" block with an empty command attribute.
Terraform
Hint
The local-exec provisioner runs commands on your local machine after resource creation.
3
Set the command to echo Server is ready
Set the command attribute inside the local-exec provisioner to the exact string echo Server is ready.
Terraform
Hint
This command will print a message on your local machine when the resource is created.
4
Add a triggers argument to force re-execution
Add a triggers argument inside the null_resource "example" block with a map containing always_run = timestamp() to ensure the provisioner runs every time.
Terraform
Hint
The triggers argument with timestamp() forces Terraform to run the provisioner every time you apply.
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.