Variable interpolation in module source block

Hi All,

I’ve been trying to keep my module sources DRY by storing all module configuration (git repo URL and version number) in a single yaml file and then referencing this file in the source parameter of each module I call. I realized that you can’t do this in terraform, so I opened a feature request here to add this capability.

Essentially, I’d like to be able to call a module with variables for the repo and reference, like this:

modules.yaml:

foo:
  repo: git@github.com:myfoomodule.git
  reference: v1.4.0

locals.tf:

module_configs = yamldecode(file("modules.yaml"))

main.tf:

module "foo" {
  source             = "${local.module_configs["foo"]["repo"]}?ref=${local.module_configs["foo"]["reference"]}"
  ...
}

Is there a way to do this, or something similar, in terragrunt?

Yes, I think you can do it almost exactly as written in Terragrunt. You’d have a modules.yml with all your source URL and versions defined and in each terragrunt.hcl, something like:

locals {
  module_configs = yamldecode(file("modules.yaml"))
}

terraform {
  source = "${local.module_configs["foo"]["repo"]}?ref=${local.module_configs["foo"]["reference"]}"
}

When you run terragrunt apply, Terragrunt will download the repo / version specified and run terraform apply in it for you.

That is fantastic! I will give this a try and see how it works. Thanks!