Terragrunt: pass a file as an argument to a terraform module

Created on 27 Oct 2017  ·  9Comments  ·  Source: gruntwork-io/terragrunt

hi,

I wan't to keep my project DRY but have a module that requires a config file as an argument (used with terraform's ${file(..)} used in a template_file datasource)

.tfvars files do not support multiline variables as per terraform documentation.
Furthermore it's often preferable to pass a full file as an argument rather than putting it inlined with EOF directive in a .tf file.

Currently the aforementioned file is contained inside the terraform's module itself, what would be my best option to move it back inside my terragrunt's live directory ?

thx,

question

Most helpful comment

for whomever would find this, the exact syntax would be:

    extra_arguments "path" {
      commands = ["${get_terraform_commands_that_need_vars()}"]
      arguments = ["-var", "file_path=${get_tfvars_dir()}/../service/config.xml"]
    }

All 9 comments

Could you update your module to take in a file_path param and to call file(var.file_path) on it? If so, you could set file_path in your .tfvars file to a path in live.

hi @brikis98,

Yes i have full control on the module, and passing the file path is an option i have considered.

But is there a way to use a file_path in the .tfvars that allows keeping a clear separation of concerns between modules' code and environment variables?

As far as i remember with terragrunt the module's code is downloaded and executed in a tmp directory so having access to the live dir would mean using an absolute path?
(let me check with a relative path inside the live dir)

It would also be best to avoid referring to the env itself in the file_path var : dev/service/config.yml would not be a good separation of concerns compared to service/config.xml

Use a relative path with one of the Terragrunt helpers to create an absolute path. Example:

terragrunt = {
  terraform {
    source = "..."

    extra_arguments "path" {
      commands = "${get_terraform_commands_that_need_vars()}"
      arguments = ["--var", "TF_VAR_file_path=${get_tfvars_dir()}/../service/config.xml"]
    }
  }
}

okkkk, i see, the solution is to pass the file path within a terragrunt block and not inside a plain .tfvars file.

Too bad it hides the declaration of this variable inside the terragrunt specific directive :
i built my project to break the terragrunt config into a terraform.tfvars that has the terragrunt = {...} block only and a component.tfvars that has the vars definition. So now I have most of the config vars inside the component.tfvars and the config file path inside the terraform.tfvars file.

Anyhow I don't see any other way around.

maybe a quick addition to the doc illustrating the use case would be worth it !

Thanks for your help 👍

cc: @rgarrigue

General-purpose interpolation in .tfvars files is an open issue, so for now, this is the best workaround. PRs to docs welcome :)

Duly noted!

Le ven. 27 oct. 2017 à 14:55, Yevgeniy Brikman notifications@github.com a
écrit :

Closed #330 https://github.com/gruntwork-io/terragrunt/issues/330.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/gruntwork-io/terragrunt/issues/330#event-1313868328,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALcep2DGgPMzx8CNw16pLEroattmChmiks5swdLYgaJpZM4QI32O
.

for whomever would find this, the exact syntax would be:

    extra_arguments "path" {
      commands = ["${get_terraform_commands_that_need_vars()}"]
      arguments = ["-var", "file_path=${get_tfvars_dir()}/../service/config.xml"]
    }

i fixed a typo above: we don't need to pass a prefixed TF_VAR_xxx var as we're directly using Terraform's -var option. (see this doc)

Ah, of course, good catch :)

Was this page helpful?
0 / 5 - 0 ratings