How to fix Error: local-exec provisioner error in Terraform
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- Verify the command exists in the local environment
- Check the command's exit code and capture output
- Verify the working directory exists and is accessible
- Use absolute paths for executables and scripts
# Test the command from your shellwhich your-command# oryour-command --versionError: 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
- 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
- File permissions prevent execution (missing execute bit on scripts, or sudo required but not available)
- Environment variables referenced in the command are not set in the Terraform execution context
- Script or command syntax differs between Unix/Linux and Windows (path separators, shell interpreters)
- Terraform Cloud/Enterprise has a different PATH or environment than local execution
How to fix "Error: local-exec provisioner error"
First, manually run the exact command from your terminal to ensure it exists and works:
# Test the command from your shell
which your-command
# or
your-command --versionIf 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).
Use on_failure = continue temporarily to see what the command outputs, then check the exit code:
provisioner "local-exec" {
command = "your-command"
on_failure = continue
}Then run:
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.
If using working_dir, ensure that directory exists:
provisioner "local-exec" {
working_dir = "/path/to/directory"
command = "your-command"
}Test this:
ls -la /path/to/directory
cd /path/to/directory && your-commandUse absolute paths instead of relative paths to avoid confusion about current working directory.
Specify the full path to the command or script:
provisioner "local-exec" {
command = "/usr/bin/bash /absolute/path/to/script.sh"
}On Windows, use full paths:
provisioner "local-exec" {
command = "powershell -Command C:\\path\\to\\script.ps1"
interpreter = ["powershell", "-Command"]
}This prevents PATH resolution issues, especially in CI/CD environments.
Pass required environment variables directly to the provisioner:
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:
export AWS_REGION=us-east-1
export APP_ENV=production
./deploy.shTerraform 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
provisioner "local-exec" {
command = "/usr/local/bin/aws s3 ls"
interpreter = ["/bin/bash", "-c"]
}Set TF_LOG to DEBUG for detailed output:
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
- 📚Terraform Official Documentation
- 📖Terraform: local-exec Provisioner Documentation(Official Docs)
- 📖Terraform: Provisioners Overview and Best Practices(Official Docs)
- 🐙