Problem
When running terraform plan (either remotely or directly in the Terraform Cloud UI), the following error message may be encountered:
Setup failed: Failed terraform init (exit 2): <nil>
Output:
sh: 1: cd: can't cd to /terraform/<$directory_name>
Cause
Once a run is triggered remotely or queued within the UI, Terraform Cloud will clone the content of the VCS repo into the /terraform
directory on the TF Cloud worker node. If there is a Terraform Working Directory defined within the UI Workspace located under General Settings
> Terraform Working Directory
Terraform will then attempt to execute cd path/to/working/directory
and will run the Terraform code located in this directory. The directory and path configured in the TFC workspace Terraform Working Directory must match the location and name within the VCS repo that is attached to the Workspace or the run will fail.
Solutions:
Make sure the directory name that is listed in the error message can't cd to /terraform/<$directory_name>
exists in your VCS repo and that the correct path to it is set for the TFC workspace Terraform Working Directory.
Outcome
When the directory and path are configured appropriately Terraform will be able to resolve the directory and its contents and the Plan/run will be successful.
Additional Information
- For additional assistance please contact HashiCorp Support to request additional assistance.