Problem
Without any changes to the Terraform configuration, runs in HCP Terraform fail with errors indicating that files or directories are missing. This can occur in several situations, including:
- The
filenameargument of a resource, such asaws_lambda_function. - The
templatefilefunction. - The
externaldata source.
Cause
HCP Terraform executes runs in ephemeral Linux-based environments. According to the run environment documentation, the specifics of this environment are an internal implementation detail and are subject to change.
Terraform Cloud performs Terraform runs in single-use Linux virtual machines, running on an x86_64 architecture. The operating system and other software installed on the worker VMs is an internal implementation detail of Terraform Cloud. It is not part of a stable public interface, and is subject to change at any time.
If your configuration contains hard-coded absolute file paths, runs may fail when HCP Terraform updates its worker environment, as those paths may no longer exist.
Solutions
Solution 1: Use Relative Paths and Correct Path References
To ensure your configurations are portable and resilient to changes in the execution environment, you must avoid absolute file paths.
- Refactor your configuration to use relative file paths instead of absolute paths.
- Replace the
path.cwdexpression withpath.rootorpath.module. Thepath.cwdexpression can resolve to an absolute path within the ephemeral worker environment, making it unreliable. In contrast,path.rootandpath.moduleprovide stable, relative paths within the context of your configuration.
Outcome
After applying these changes, HCP Terraform runs should complete successfully without encountering file path errors.