Problem
When you trigger a run in HCP Terraform, either remotely via the CLI or directly in the UI, the plan may fail with an error message indicating that no Terraform configuration files were found.
Error: No Terraform configuration files found in working directory Terraform requires configuration files to plan changes. Planning without a configuration would mark everything for destruction, which is normally not what is desired. If you would like to destroy everything, queue a destroy plan instead. Otherwise, create a Terraform configuration file (.tf file), or check that the working directory is set correctly on the workspace, and try again. Operation failed: failed checking for terraform files: lstat /home/tfc-agent/.tfc-agent/component/terraform/runs/run-XXXXXXXXXXXXXXXX/config/my_terraform_working_dir: no such file or directory
Cause
When a run begins, HCP Terraform clones the contents of your version control system (VCS) repository into a temporary directory on the execution agent, such as /home/tfc-agent/.tfc-agent/component/terraform/runs/run-XXXXXXXXXXXXXXXX/config/.
If you have specified a path in the workspace's General Settings > Terraform Working Directory, HCP Terraform will attempt to change to that subdirectory before executing terraform plan. The error occurs when the path specified in the Terraform Working Directory setting does not exist in the cloned VCS repository.
Solution
Solution 1: Verify the Terraform Working Directory Path
Ensure the path configured in your HCP Terraform workspace's Terraform Working Directory setting exactly matches a directory path that exists within your VCS repository.
- Review the error message to identify the directory that HCP Terraform could not find. In the example above, this is
my_terraform_working_dir. - Navigate to your VCS repository and confirm that this directory exists at the expected path.
- In the HCP Terraform UI, go to your workspace settings and verify that the value for
Terraform Working Directoryis correct. If it is incorrect, update it to match the actual path in your repository.
Outcome
After you correct the Terraform Working Directory path to match the structure of your VCS repository, new runs will successfully locate the Terraform configuration files and proceed with the plan.