How to upload a non-terraform file to Terraform cloud

I have a Terraform configuration that that references some non-terraform files. When I try to apply this configuration on a workspace with remote execution, terraform fails with an error like this:

Error: Invalid function argument

  on static_site_cdn.tf line 89, in resource "aws_cloudfront_function" "static_web_request_function":
  89:   code = file("${local.bin_root}/cloudfront_function/static_website_request_function.js")
    |----------------
    | local.bin_root is "./../bin"

Invalid value for "path" parameter: no file exists at
../bin/cloudfront_function/static_website_request_function.js; this function
works only with files that are distributed as part of the configuration source
code, so if this file will be created by a resource in this configuration you
must instead obtain this result from an attribute of that resource.

Since static_website_request_function.js is not created by a resource in the configuration, the suggestion in the error message isn’t relevant. I’d like to know if it is possible to distribute my static_website_request_function.js file along with my terraform config.

Thanks!

Hi @amcgregor101,

When you run an command with remote operations, Terraform CLI will create an archive of a subtree of your local filesystem and upload it to Terraform Cloud, and then Terraform Cloud will in turn then extract that archive in order to create the filesystem to run the remote Terraform CLI inside.

Unless you have overridden things with the .terraformignore file, Terraform CLI will include all files in the directory except for some special files that are relevant to Terraform CLI itself, inside the .terraform directory. That should include, for example, arbitrary .js files you might have included there.

However, since your path includes ../ I expect the problem is instead that the subtree uploaded to Terraform Cloud starts at your current directory and therefore doesn’t include anything from your parent directory, and so ../bin isn’t present in the remote execution environment at all.

I think the solution to this, then, will be to change your workspace configuration in Terraform Cloud to set a working directory. I can’t see from your question the name of the directory that contains static_site_cdn.tf, but for the sake of example let’s assume your directory structure is shaped like this:

  • bin
    • cloudfront_function
      • static_website_request_function.js
  • terraform
    • static_site_cdn.tf

That is, that the static_site_cdn.tf file is inside a directory called terraform alongside bin.

In that case, you’d configure the working directory as terraform, which will then tell Terraform CLI that the configuration for this workspace is in a subdirectory of its filesystem tree, and so it’ll archive and upload the contents of ../ rather than just of the terraform directory alone.

This mechanism is admittedly a little confusing if you are primarily using Terraform CLI to interact with Terraform Cloud. To help think about it, imagine that Terraform Cloud were instead directly cloning whatever VCS repository these files belong to, and then set the working directory to be whatever subdirectory path is necessary to get from the root of that repository to the directory where the static_site_cdn.tf file is. Terraform CLI here is attempting to create the effect of cloning that VCS repository, but gathering the files up client-side so that you can work with changes you’ve not necessarily pushed to the remote repository yet.

Thanks for the detailed response. The issue was the working directory.