Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass a file as an argument to a terraform module #330

Closed
ebarault opened this issue Oct 27, 2017 · 9 comments
Closed

pass a file as an argument to a terraform module #330

ebarault opened this issue Oct 27, 2017 · 9 comments
Labels

Comments

@ebarault
Copy link
Contributor

ebarault commented Oct 27, 2017

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,

@brikis98
Copy link
Member

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.

@ebarault
Copy link
Contributor Author

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

@brikis98
Copy link
Member

brikis98 commented Oct 27, 2017

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"]
    }
  }
}

@ebarault
Copy link
Contributor Author

ebarault commented Oct 27, 2017

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

@brikis98
Copy link
Member

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

@ebarault
Copy link
Contributor Author

ebarault commented Oct 27, 2017 via email

@ebarault
Copy link
Contributor Author

ebarault commented Oct 30, 2017

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"]
    }

@ebarault
Copy link
Contributor Author

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)

@brikis98
Copy link
Member

Ah, of course, good catch :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants