0
0
Terraformcloud~3 mins

Why Local-exec provisioner in Terraform? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your computer could finish the boring setup work for you every time you change cloud resources?

The Scenario

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.

The Problem

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 Solution

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.

Before vs After
Before
terraform apply
# Then manually run: scp file.txt user@server:/path
# Then ssh user@server 'sudo systemctl restart service'
After
resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "scp file.txt user@server:/path && ssh user@server 'sudo systemctl restart service'"
  }
}
What It Enables

You can automate local tasks triggered by cloud changes, making your deployments faster and error-free.

Real Life Example

After launching a new virtual machine, automatically copying configuration files from your laptop and restarting services without lifting a finger.

Key Takeaways

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.