Bird
Raised Fist0
Terraformcloud~5 mins

Local-exec provisioner in Terraform - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to run a command on your own computer right after creating a resource in the cloud. The local-exec provisioner lets Terraform run these commands automatically. This helps you automate tasks like configuring software or notifying people.
When you want to run a script on your local machine after creating a cloud resource.
When you need to send a notification email or message after deployment.
When you want to run a local command to update a configuration file after resource creation.
When you want to execute a local shell command to prepare your environment for the new resource.
When you want to automate running tests or checks on your local system after infrastructure changes.
Config File - main.tf
main.tf
resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "echo 'Resource created successfully'"
  }
}

This Terraform file creates a dummy resource called null_resource.example. It uses the local-exec provisioner to run a local shell command.

The command key defines the exact command to run on your local machine after the resource is created.

Commands
This command initializes the Terraform working directory. It downloads necessary plugins and prepares Terraform to run.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/null... - Installing hashicorp/null v3.1.0... - Installed hashicorp/null v3.1.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command applies the Terraform configuration to create the resource and runs the local-exec command automatically. The -auto-approve flag skips manual approval.
Terminal
terraform apply -auto-approve
Expected OutputExpected
null_resource.example: Creating... null_resource.example: Provisioning with 'local-exec'... null_resource.example (local-exec): Executing: ["/bin/sh" "-c" "echo 'Resource created successfully'"] null_resource.example (local-exec): Resource created successfully null_resource.example: Creation complete after 0s [id=1234567890] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
→
-auto-approve - Automatically approve the apply without prompting
This command removes the created resource and cleans up your environment. The -auto-approve flag skips manual confirmation.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
null_resource.example: Destroying... null_resource.example: Destruction complete after 0s Destroy complete! Resources: 1 destroyed.
→
-auto-approve - Automatically approve the destroy without prompting
Key Concept

If you remember nothing else from this pattern, remember: local-exec runs commands on your own computer right after Terraform creates or changes a resource.

Common Mistakes
Using local-exec to run commands that require the resource to be fully ready remotely.
Local-exec runs commands locally and does not wait for remote services to be fully operational, which can cause failures.
Use remote-exec provisioner or other tools to run commands on the remote resource after it is ready.
Not specifying the full command or using incorrect shell syntax in the command string.
Terraform will fail to run the command if the syntax is wrong or the command is incomplete.
Test your command locally in a shell before adding it to the provisioner.
Relying on local-exec for critical infrastructure setup that should be idempotent and repeatable.
Local-exec commands may not be idempotent and can cause inconsistent states if run multiple times.
Use Terraform resources or configuration management tools for critical setup; use local-exec only for simple local tasks.
Summary
Initialize Terraform with 'terraform init' to prepare the environment.
Use 'terraform apply' to create resources and run local commands automatically.
Use 'terraform destroy' to clean up resources when no longer needed.

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

  1. Step 1: Understand provisioner types

    Terraform provisioners run scripts or commands either locally or remotely after resource creation.
  2. Step 2: Identify local-exec purpose

    The local-exec provisioner specifically runs commands on your local machine, not inside the cloud resource.
  3. Final Answer:

    Runs commands on your local machine after resource creation -> Option D
  4. 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

  1. Step 1: Recall Terraform provisioner syntax

    Provisioners use the keyword provisioner followed by the provisioner type in quotes.
  2. Step 2: Check correct attribute names

    The local-exec provisioner uses command to specify the shell command to run.
  3. Final Answer:

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

    Correct syntax uses provisioner "local-exec" and command [OK]
Hint: Use quotes and 'command' attribute for local-exec [OK]
Common Mistakes:
  • Omitting quotes around local-exec
  • Using wrong attribute names like run or execute
  • Using underscore instead of dash in local-exec
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' inside the cloud resource
C. Terraform will print 'Resource created' on your local machine
D. Terraform will ignore the provisioner and create the resource silently

Solution

  1. Step 1: Understand null_resource and local-exec

    The null_resource is a Terraform resource that supports provisioners for running commands.
  2. Step 2: Analyze the command execution

    The local-exec provisioner runs the command on your local machine, so it will print the message locally.
  3. Final Answer:

    Terraform will print 'Resource created' on your local machine -> Option C
  4. Quick Check:

    local-exec runs commands locally, so output appears locally [OK]
Hint: local-exec runs commands locally, not remotely [OK]
Common Mistakes:
  • Thinking output appears inside cloud resource
  • Believing null_resource disallows provisioners
  • Assuming provisioners are ignored silently
4. You wrote this Terraform code but get an error when applying:
resource "null_resource" "fail_example" {
  provisioner "local-exec" {
    command = echo "Hello"
  }
}
What is the cause of the error?
medium
A. Missing quotes around the command string
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

  1. Step 1: Check command syntax

    The command value must be a string enclosed in quotes in Terraform configuration.
  2. Step 2: Identify missing quotes

    Here, command = echo "Hello" lacks quotes around the entire command string, causing a syntax error.
  3. Final Answer:

    Missing quotes around the command string -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Use remote-exec provisioner to run setup.sh on the EC2 instance -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard