Terragrunt: Interpolation variables in modules

Created on 30 Aug 2018  路  8Comments  路  Source: gruntwork-io/terragrunt

When call the tf module, I used to feed some dynamic variables to that module

module xxx {
  source = "..."
  name = "${local.name_prefix}-xxx"
  file = "${data.template_file.setting.rendered}"
  ...
  vpc_id = "${data.terraform_remote_state.vpc.vpc_id}"
  account_id   = "${data.aws_caller_identity.current.account_id}"
  ...
}

Terraform does NOT process interpolations in .tfvars files. So how can I handle these in terraform.tfvars with terragrunt?

question

All 8 comments

The code you have above _is_ the module you'd deploy with Terragrunt. That is, you'd create, say, modules/my-module/main.tf, and in it, call module "xxx" { ... }, fill in variables, use interpolations, etc. Anything that varies between environments would be exposed as a variable in modules/my-module/vars.tf. You would then use Terragrunt to deploy modules/my-module and the variables you define in your terraform.tfvars would not use/need any interpolations.

See these example repos:

https://github.com/gruntwork-io/terragrunt-infrastructure-modules-example
https://github.com/gruntwork-io/terragrunt-infrastructure-live-example

Thanks @brikis98

Maybe I didn't explain my problem clearly

I knew how to use basic terragrunt, but when deal with the module's variables, I can't directly write terraform.tfvars as below.

terragrunt = {
  terraform {
    source = "modulexxxx"
  }

  include = {
    path = "${find_in_parent_folders()}"
  }
}

name="${local.name_prefix}-xxx"
file ="${data.template_file.setting.rendered}"
vpc_id="${data.terraform_remote_state.vpc.vpc_id}"
account_id="${data.aws_caller_identity.current.account_id}"
another_name="${var.another_name}"

I give 5 samples which I used to reference tf module before, but how to do that with terragrunt?

I just answered that above. You put all the interpolations into modulexxxx. Not into terraform.tfvars. Please look at the examples.

That will be inconvenience. It changes the way we write Terraform.

I don't think it does. If you already have modulexxx, and you're using it in your Terraform code foo, then somewhere in foo, you are using interpolations. All I'm saying is point Terragrunt at foo.

Do you mean, if I need interpolation variables in a module, then I have to write a wrapper module to call that original module, this is weird.

Maybe I didn't fully understand your explanation, let me think again.

Yes, that's exactly what I mean. You write a wrapper module that configures things the way you need them for your deployment: i.e., it configures the provider, terraform settings (e.g., min version, backends), terraform_remote_state, variables, outputs, etc.

most my tf modules need use interpolation variables ...

Seems this is the design in terragrunt. Looks nothing can do in current situation.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings