How to fix Error: local-exec provisioner error in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 30, 2025· 145 views

The local-exec provisioner error occurs when Terraform fails to execute a local command on the machine running Terraform. Common causes include missing commands in PATH, non-zero exit codes, permission issues, or working directory problems. Debugging requires checking command availability, exit codes, and execution environment.

Quick fix

Fixes Error: local-exec provisioner error

# Test the command from your shell
which your-command
# or
your-command --version
  1. Verify the command exists in the local environment
  2. Check the command's exit code and capture output
  3. Verify the working directory exists and is accessible
  4. Use absolute paths for executables and scripts
terraform — error
# Test the command from your shell
which your-command
# or
your-command --version
Error: local-exec provisioner error

What this error means

The local-exec provisioner invokes scripts or commands on the local machine that runs Terraform—not on a remote resource. When this provisioner fails, Terraform displays "Error: local-exec provisioner error" with additional context. The error typically stems from one of three sources: the specified command doesn't exist in the execution environment's PATH, the command executes but returns a non-zero exit code (indicating failure), or environmental issues like incorrect working directory or permission problems prevent execution. Unlike remote-exec provisioners which connect over SSH or WinRM, local-exec failures happen directly on your workstation or CI/CD runner, making them easier to debug but sometimes harder to diagnose in automated environments like Terraform Cloud.

Typical symptoms

  • Terraform apply or destroy exits with 'Error: local-exec provisioner error'
  • Error message shows 'exit status 127' (command not found) or other non-zero codes
  • Command works fine when run manually in your terminal but fails in Terraform
  • CI/CD pipeline fails at the local-exec provisioner step even though code is identical to local runs
  • Working directory or file paths in the provisioner don't resolve correctly

Common causes

  1. Command is not in the system PATH of the Terraform execution environment (especially common in CI/CD where PATH differs from your local shell)
  2. Command exits with a non-zero status code, which Terraform treats as a failure regardless of actual success
  3. Working directory specified in the provisioner does not exist or is inaccessible
  4. File permissions prevent execution (missing execute bit on scripts, or sudo required but not available)
  5. Environment variables referenced in the command are not set in the Terraform execution context
  6. Script or command syntax differs between Unix/Linux and Windows (path separators, shell interpreters)
  7. Terraform Cloud/Enterprise has a different PATH or environment than local execution

How to fix "Error: local-exec provisioner error"

1Verify the command exists in the local environment

First, manually run the exact command from your terminal to ensure it exists and works:

bash
# Test the command from your shell
which your-command
# or
your-command --version

If the command doesn't exist, you need to install it or update your PATH. For Terraform Cloud or CI/CD, ensure the command is installed in that environment (not just your local machine).

2Check the command's exit code and capture output

Use on_failure = continue temporarily to see what the command outputs, then check the exit code:

hcl
provisioner "local-exec" {
  command    = "your-command"
  on_failure = continue
}

Then run:

bash
terraform apply
your-command; echo "Exit code: $?"

If the exit code is non-zero, the command is failing. Verify it succeeds independently before adding to Terraform.

3Verify the working directory exists and is accessible

If using working_dir, ensure that directory exists:

hcl
provisioner "local-exec" {
  working_dir = "/path/to/directory"
  command     = "your-command"
}

Test this:

bash
ls -la /path/to/directory
cd /path/to/directory && your-command

Use absolute paths instead of relative paths to avoid confusion about current working directory.

4Use absolute paths for executables and scripts

Specify the full path to the command or script:

hcl
provisioner "local-exec" {
  command = "/usr/bin/bash /absolute/path/to/script.sh"
}

On Windows, use full paths:

hcl
provisioner "local-exec" {
  command    = "powershell -Command C:\\path\\to\\script.ps1"
  interpreter = ["powershell", "-Command"]
}

This prevents PATH resolution issues, especially in CI/CD environments.

5Set environment variables explicitly if the command needs them

Pass required environment variables directly to the provisioner:

hcl
provisioner "local-exec" {
  command = "deploy.sh"
  environment = {
    AWS_REGION = var.aws_region
    APP_ENV    = "production"
  }
}

Verify the command runs with these variables in your shell:

bash
export AWS_REGION=us-east-1
export APP_ENV=production
./deploy.sh
6For Terraform Cloud, ensure the command is available in the remote runner

Terraform Cloud/Enterprise runs provisioners in a sandboxed environment with a limited PATH. If using local-exec in Terraform Cloud:

1. Use absolute paths (e.g., /usr/bin/aws instead of just aws)
2. Use the built-in interpreter option to call a shell explicitly
3. Consider pre-installing tools via a cloud init or provider setup
4. Check the Terraform Cloud logs for actual error messages

hcl
provisioner "local-exec" {
  command    = "/usr/local/bin/aws s3 ls"
  interpreter = ["/bin/bash", "-c"]
}
7Enable detailed logging for better debugging

Set TF_LOG to DEBUG for detailed output:

bash
export TF_LOG=DEBUG
terraform apply 2>&1 | grep -A 10 "local-exec"

The logs will show the exact command being executed and any error output. This is especially helpful in CI/CD pipelines.

Advanced notes

Provisioner Best Practices

While local-exec is useful for post-apply hooks, HashiCorp recommends avoiding provisioners when possible. Provisioners are harder to debug, can make infrastructure less reproducible, and introduce dependencies on the local execution environment. Consider these alternatives:

- null_resource with triggers: Use null_resource for orchestration without creating actual resources
- User data scripts: For cloud resources, use cloud-init or user data scripts instead
- Configuration management: Use dedicated tools like Ansible, Chef, or Puppet
- Cloud functions/lambdas: Trigger post-deployment logic via cloud services

Cross-Platform Compatibility

Commands that work on macOS/Linux may fail on Windows due to path separators and shell differences. Always test on the target platform or use the interpreter option to specify an explicit shell.

Terraform Cloud Limitations

In Terraform Cloud, the execution environment is restricted and may not have the same tools or PATH as your local machine. Always test your provisioners locally before running in Terraform Cloud, and prefer tools that are pre-installed in the TFC runner environment.

Frequently asked questions

What does "Error: local-exec provisioner error" mean?
The local-exec provisioner invokes scripts or commands on the local machine that runs Terraform—not on a remote resource. When this provisioner fails, Terraform displays "Error: local-exec provisioner error" with additional context. The error typically stems from one of three sources: the specified command doesn't exist in the execution environment's PATH, the command executes but returns a non-zero exit code (indicating failure), or environmental issues like incorrect working directory or permission problems prevent execution.
How do I fix "Error: local-exec provisioner error"?
The local-exec provisioner error occurs when Terraform fails to execute a local command on the machine running Terraform. Common causes include missing commands in PATH, non-zero exit codes, permission issues, or working directory problems. Debugging requires checking command availability, exit codes, and execution environment.
What causes "Error: local-exec provisioner error"?
Command is not in the system PATH of the Terraform execution environment (especially common in CI/CD where PATH differs from your local shell) Command exits with a non-zero status code, which Terraform treats as a failure regardless of actual success Working directory specified in the provisioner does not exist or is inaccessible
Is the "Error: local-exec provisioner error" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading