0
0
Terraformcloud~5 mins

Local-exec provisioner in Terraform - Commands & Configuration

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