Hi @mikek,
A key thing to know about count vs. for_each is that a resource with count set will appear as a list when you refer to it elsewhere in the module, while a resource with for_each appears as a map. In the for_each case, the map keys are the same as the keys in the map you pass to for_each. For example:
resource "null_resource" "example" {
for_each = {
"a" = 1
"b" = 2
}
}
In the above contrived example, null_resource.example elsewhere in the module would produce a map with the keys a and b and with the values set to objects representing each of the resource instances.
It sounds like you want your module to return an output value that summarizes the map of the resource instances for external consumption, using a map. If so, then a for expression is indeed the usual choice. The splat operator is essentially a special case of for expression that works only with lists and sets, but we can use a for expression with any collection or structure type.
Here’s a less-contrived example:
variable "subnet_cidr_blocks" {
type = map(string)
}
resource "aws_subnet" "example" {
for_each = var.subnet_cidr_blocks
cidr_block = each.value
# ...
}
output "subnet_ids" {
value = { for k, s in aws_subnet.example : k => s.id }
}
From the perspective of the calling module then, the subnet ids map would be accessible as module.example.subnet_ids and the keys of that map would be the same keys the caller passed in the subnet_cidr_blocks map, allowing the caller to correlate them.
You can also produce a more complex result that aggregates together several different attributes, if you like:
output "subnets" {
value = {
for k, s in aws_subnet.example : k => {
id = s.id
cidr_block = s.cidr_block
availability_zone = s.availability_zone
}
}
}
This would then appear to the caller as a map of objects, again with the same keys as given in the subnet_cidr_blocks variable.
I’m not sure how the question about depends_on relates to the rest of it, but indeed you can refer to a specific module output in a depends_on like that. If you do, the resource in question will depend indirectly on anything the output value depends on, which in my example above would be the aws_subnet resources inside the module.