Terraform: output from multiple module calls

1.2k views Asked by At

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.

1

There are 1 answers

3
Martin Atkins On

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.