Dotan
Dotan

Reputation: 7632

Terraform: using the file interpolation to read local file from a module

Is there a way to tell terraform that when I do something like

locals {
  file_content = "${file("somefile.txt")}"
}

Inside a module, it sould read the file that's in the module?

It appears that if the file structure is this:

/module
  /main.tf        <- the code above is here
  /somefile.txt
/project
  /main.tf        <- this uses the module from ../module

Terraform will look for the file under /project and not under /module

Thanks

Upvotes: 3

Views: 496

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56997

You can use path.module to refer to the path of the module.

So in your module you would refer to it with:

locals {
  file_content = "${file("${path.module}/somefile.txt")}"
}

This is also shown in the documentation for the file function:

> file("${path.module}/hello.txt")
Hello World

Upvotes: 3

Related Questions