Module output in a for_each module

I’m hoping someone can explain to me what is happening in my example, as the behaviour was very confusing to me when I was testing this.

What I wanted to do is get the output of 1 instance of a for_each looped module and use that output as a variable in another module. To test this I had the following setup:

#./dummymodule/outputs.tf
output "test1" {
    value = "foo"
}
output "test2" {
    value = "bar"
}

#./main.tf
terraform {
  backend "local" {}
}
module "dummy" {
  source = "./dummymodule"
  for_each = toset(["test1", "test2"])
}
output "output_of_dummy_module" {
  value = module.dummy.*
}

The output of this is what I expected:

output_of_dummy_module = [
  {
    "test1" = {
      "test1" = "foo"
      "test2" = "bar"
    }
    "test2" = {
      "test1" = "foo"
      "test2" = "bar"
    }
  },
]

For my use case I then needed to get the test1 output of the test1 instance (bare with me with the naming here, it is required to demonstrate the behaviour) of this module, so I changed the value of output_dummy_module to module.dummy.test1.test1. Again the output is what I expected:

output_of_dummy_module = "foo"

When I tried to implement this into my actual setup, the names of all my blocks were more unique, so it would be something like:

#./dummymodule/outputs.tf
output "output1" {
    value = "foo"
}
output "output2" {
    value = "bar"
}

#./main.tf
terraform {
  backend "local" {}
}
module "dummy" {
  source = "./dummymodule"
  for_each = toset(["zone1", "zone2"])
}
output "output_of_dummy_module" {
  value = module.dummy.zone1.output1
}

This gave me an error:

│ Error: Unsupported attribute
│
│   on main.tf line 16, in output "output_of_dummy_module":
│   16:   value = module.dummy.zone1.output1
│     ├────────────────
│     │ module.dummy is object with no attributes
│
│ This object does not have an attribute named "zone1".

After some testing, I noticed that as long as one of the output names is equal to the module instance I’m trying to get the output value of, it would work and if not, it would throw the above error. To demonstrate this, changing output2 (which isn’t called in the root module at all) to zone1 and not changing anything else, I would again get the value of output1;

output_of_dummy_module = "foo"

I am very curious what is happening here, if anyone can explain.

% terraform --version
Terraform v1.0.11
on darwin_amd64

P.S. For anyone trying to do the same thing I wanted to do; in the end I realised that in order to get the output as I needed, I had to get the output value as such:

output "output_of_dummy_module" {
  value = module.dummy["zone1"].output2
}

Which also allowed me to replace the zone1 string with a variable.