Using Templates with Modules imported via Git

I am trying to create a Datadog datadog_dashboard_json resource that consumes a templatefile JSON to easily provision a dashboard based on a JSON exported from Datadog itself. I want this to be a reusable module i import via git, but the templatefile function attempts to load the relative path with respect to the consuming module. i.e., if my dashboard is in Dashboard_Module and is consumed by Project_Module, templatefile tries to find my dashboard template file with respect to Project_Module.

Is there a way to use a template JSON within Dashboard_Module that Dashboard_Module will always load so that I can have a convenient reusable dashboard?

Hi @nlee,

The various functions that read files directly from disk, which includes templatefile, always interpret the path relative to the root module directory so that it’s possible to pass paths between modules without confusion about what they were relative to.

You can make a path relative to the current module by including the value of path.module in the path string. path.module is the path of the current module relative to the path of the root module.

For example:

templatefile("${path.module}/example.tmpl", {})

Since your reusable module is being loaded from a remote source, in practice path.module will be a path something like ./.terraform/modules/Dashboard_Module, which points into the hidden directory where terraform init creates a cache of remote modules so that other Terraform commands can refer to them. The function should therefore be able to find the file and load it from the cache directory.

1 Like