Terraform: file function not finding files in modules

Created on 19 Feb 2016  ·  11Comments  ·  Source: hashicorp/terraform

I have a module, where I'm loading user-data for a launch config like this:

user_data = "${file("mgt-user-data.sh")}"

The file "mgt-user-data.sh" is in the same directory as the .tf that calls it, and both are gotten during terraform get.

However, where planned the following error comes back:

  • file: open mgt-user-data.sh: no such file or directory in:

${file("mgt-user-data.sh")}

This works perfectly in non-module .tf that I have, but fails every time in a called module.

The file function should, IMHO, base relative path resolution on the path of the caller, but it does not appear to be doing that (otherwise it would find the file listed above).

core question

Most helpful comment

Hi @joseph-wortmann,

Can you try this?

user_data = "${file("${path.module}/mgt-user-data.sh")}"

and see if that works for you?

Paul

All 11 comments

It also will not work with relative paths in the main .tf, UNLESS terraform is run within the .tf directory. if run from outside the directory, it will fail.

The only way the file function appears to behave reliably is if absolute paths are used.

Hi @joseph-wortmann,

Can you try this?

user_data = "${file("${path.module}/mgt-user-data.sh")}"

and see if that works for you?

Paul

Thanks Paul,

That worked. Thank you.

However, that seems very non-intuitive. I think that relative paths should
do something like this automatically for users, or at the very least this
syntax should be added as an example to the documentation. My 2 cents.

Regards,

Joseph Wortmann
(205) 612-3826

On Fri, Feb 19, 2016 at 7:30 AM, Paul Stack [email protected]
wrote:

Hi @joseph-wortmann https://github.com/joseph-wortmann,

Can you try this?

user_data = "${file("${path.module}/mgt-user-data.sh")}"

and see if that works for you?

Paul


Reply to this email directly or view it on GitHub
https://github.com/hashicorp/terraform/issues/5213#issuecomment-186213954
.

@joseph-wortmann - glad this worked. I will leave this issue open and speak to some of the other contributors and get their thoughts

I just reviewed the docs around this and it's definitely confusing as is! Adding explicit mention of the way path is interpreted to the file() docs and will link back here.

Updated docs landed on the site! Let me know if there's anything further we can do to clarify the behavior, @joseph-wortmann. :+1:

Thank you!

Regards,

Joseph Wortmann
(205) 612-3826

On Fri, Feb 19, 2016 at 3:07 PM, Paul Hinze [email protected]
wrote:

Updated docs landed on the site! Let me know if there's anything further
we can do to clarify the behavior, @joseph-wortmann
https://github.com/joseph-wortmann. [image: :+1:]


Reply to this email directly or view it on GitHub
https://github.com/hashicorp/terraform/issues/5213#issuecomment-186408384
.

I had this same issue using the file provisioner. Using $path.module does help resolve this.

Had the same issue involving:

provisioner "file" {
    source = "./install_docker.sh"
    destination = "/tmp/install_docker.sh"
    connection {
      type = "ssh",
      user = "root",
      private_key = "${file(var.ssh_private_key)}"
    }
  }

Worked as suggested by using $path.module:

provisioner "file" {
    source = "${path.module}/install_docker.sh"

Can we consider it as "normal" or should we work on this "old" issue ?
Maybe just missing documentation (or maybe I dont find it) ?

Involving filemd5 its same:
etag = "${filemd5("scripts/bootstrap_actions.sh")}"

Resolved also by using $path.module::
etag = "${filemd5("${path.module}/scripts/bootstrap_actions.sh")}"

Thanks to @stack72 !

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings