Introduction
Terraform Agents are used with Terraform Enterprise to allow communication with isolated, private, or on-premises infrastructure.
Terraform Agents support running custom programs called hooks.
Problem
A hook custom script contains the command 'terraform init' and the terraform configuration code uses public modules.
Example script:
#!/bin/bash
terraform init
When the hook script is triggered and terraform init runs, the script it exits with an error:
Error: Failed to download module
With TRACE log level enabled for the terraform agent, the error in TRACE logs is similar to this:
git must be available and on the PATH
But if you connect to the terraform agent host with ssh:
- Git is installed
- Run 'terraform init' manually, it runs without errors.
Prerequisites
- Terraform Cloud or Terraform Enterprise
- TFC agent hooks
Cause
- The custom hook script does not have an environment variable set for PATH, so git needs to be added to the PATH.
Solutions:
Connect to the host machine or docker container of the terraform agent.
Find the PATH to git command, run:
which git
example output:
/usr/bin/git
Edit the custom hook script and and the PATH to git.
export PATH=$PATH:/usr/bin
The example script should now look like this:
#!/bin/bash
export PATH=$PATH:/usr/bin
terraform init
Restart the terraform agent for the new script to take effect.
Outcome
When the custom hook script runs 'terraform init' downloads the modules without errors.