1

Pseudo-code:

module "foo-1" {
    source="./foo"
    input=1
}
module "foo-2" {
    source="./foo"
    input=2
}
module "foo-3"
    source="./foo"
    input=3
}
...etc...

(The module ./foo outputs a unique id based on the input value)

Problem:

I would like to be able to arbitrarily instantiate/call the ./foo module and have access to the unique id from each module instances. I can't see a way to do this with Terraform as the output syntax either requires a unique val=expression per module instantiation. Splat expressions on the module object (module.*.id) are unfortunately (and not surprisingly) not supported.

I'm guessing that this can't be done in terraform but would love to be wrong.

Brett Monroe
  • 151
  • 1
  • 7

1 Answers1

0

Because each of those modules is entirely separate from Terraform's perspective, to gather their results together into a single value will require writing an expression to describe that. For example:

locals {
  foos = [
    module.foo_1,
    module.foo_2,
    module.foo_3,
  ]
}

With a definition like that, elsewhere in your module you can then write an expression like local.foos[*].id to collect up all of the ids across all of the modules.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138
  • Thanks for the followup and suggestion, Martin. Unfortunately, w/r to the above, it requires an update to the local array every time a new module instance is called which is what I'm trying/hoping to avoid as, in my real use case, each child module instance is in it's own .tf file in the calling module. – Brett Monroe Oct 21 '20 at 14:22
  • There is no automatic way for Terraform to understand that these modules all go together, so you have to tell it somehow. One alternative would be to use `count` or `for_each` in a single module block to systematically define multiple instances of the same module based on a collection, assuming their input variable definitions are similar enough to all come from the same source expressions. – Martin Atkins Oct 22 '20 at 23:49
  • Thanks again, Matin. Yeah, I thought about loop operations but they won't work within the confines of the architecture nor in the output clock (as there's no way to collect all the modules). I may just live with it until we are ready to re-architect the root module (setup the way it is to user TFE and minimize workspace license usage. – Brett Monroe Oct 28 '20 at 16:18