Local-exec Provisioner in Terraform: What It Is and How It Works
local-exec provisioner in Terraform runs commands on the machine where Terraform is executed, right after a resource is created or updated. It helps automate tasks like running scripts or commands locally as part of your infrastructure deployment.How It Works
The local-exec provisioner acts like a helper that runs commands on your own computer or wherever you run Terraform. Imagine you just built a new house (a resource), and you want to immediately turn on the lights or set up the Wi-Fi yourself. The local-exec provisioner is like you doing those tasks right after the house is ready.
When Terraform finishes creating or updating a resource, it triggers the local-exec provisioner to run the command you specify. This command runs locally, not on the cloud or remote server. It can be anything from running a script, copying files, or printing messages.
This helps connect your infrastructure setup with local tasks that need to happen automatically, making your deployment smoother and more complete.
Example
This example shows how to use the local-exec provisioner to run a simple shell command after creating a null resource.
resource "null_resource" "example" { provisioner "local-exec" { command = "echo 'Hello from local-exec provisioner!'" } }
When to Use
Use the local-exec provisioner when you need to run commands or scripts on your local machine as part of your Terraform deployment. This is useful for tasks like:
- Running setup scripts that configure local tools or environments.
- Triggering notifications or logging after resource creation.
- Copying files or generating configuration files locally.
It is best for actions that cannot be done directly by Terraform resources or remote provisioners, especially when the task must happen on the machine running Terraform.
Key Points
- Runs locally: Commands execute on the machine running Terraform, not on remote servers.
- Triggered after resource actions: Runs after resource creation or update.
- Flexible commands: Can run any shell command or script.
- Use carefully: Avoid complex logic; better for simple local tasks.