This error occurs when a Terraform provisioner (local-exec, remote-exec, or file) tries to execute a command that doesn't exist in the target environment's PATH. This typically happens when commands are called by name rather than full path, or when dependencies aren't available in Terraform Cloud environments.
When Terraform executes provisioners, they rely on commands available in the system PATH. If a command (like gcloud, ssh-keygen, or any custom script) is not found in the expected PATH, the provisioner fails with exit status 127. This is especially common in local-exec provisioners running in Terraform Cloud where the execution environment may differ from your local machine, or when provisioners reference commands that aren't installed or aren't in the expected location.
Replace the command name with its absolute path. For example, instead of:
provisioner "local-exec" {
command = "gcloud auth list"
}Use:
provisioner "local-exec" {
command = "/usr/bin/gcloud auth list"
}Find the full path using which <command> on Linux/macOS or where <command> on Windows.
Use the interpreter argument to ensure commands run in a known shell environment:
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = "echo 'Running in bash'"
}This is especially important when using shell-specific syntax like variable expansion or pipes.
Use the working_dir argument to ensure relative paths resolve correctly:
provisioner "local-exec" {
command = "./deploy.sh"
working_dir = "${path.module}/scripts"
}Ensure the directory exists before the provisioner runs.
If running in Terraform Cloud, ensure all required tools are available in that environment. For the gcloud example, you may need to:
provisioner "local-exec" {
command = "curl https://sdk.cloud.google.com | bash"
}
provisioner "local-exec" {
command = "gcloud auth list"
}Alternatively, use a custom Docker image or build step that includes required tools.
When referencing your own scripts, use absolute paths instead of relative paths:
provisioner "local-exec" {
command = "${path.module}/scripts/deploy.sh"
}Never rely on the current working directory for script resolution.
Use on_failure = "continue" temporarily to see error output without stopping the entire apply:
provisioner "local-exec" {
command = "failing-command"
on_failure = "continue"
}This helps you diagnose PATH or environment issues without rolling back resources. Once fixed, remove this argument or set it back to "fail".
Exit status 127 is the standard Unix convention for 'command not found'. On remote systems with restrictive shells or limited PATH configurations, all relative command resolution fails. For remote-exec provisioners, ensure the target instance has the necessary commands installed and that the connection user has permission to execute them. In Terraform Cloud, the execution happens in a stateless, minimal Linux environment—many system utilities and programming tools are not available by default. Always prefer explicit paths over PATH resolution. When using remote-exec on instances where you control the image, pre-bake required tools into the AMI or container to avoid provisioner failures.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform