I need to combine the results of a variable and an output (from another module) within a module I am creating for NLB’s. The combined local would then be used in a for_each loop.
I’ve been trying to follow the setproduct example, but only get so far.
What I am trying to do:
I get the output from an EC2 module for the Instance ID:
output "ec2_details" {
description = "ID from EC2 Resource"
value = tomap({
for k, i in aws_instance.this : k => {
id = i.id
}
})
}
This is then passed into a NLB module and I want to combine it with the variable I pass to the new module:
variable "nlb_target_group" {
type = map(object({
nlb_target_name = string
nlb_target_port = string
nlb_target_protocol = string
nlb_target_type = string
}))
}
The combined local (call it local.combined) would then be used in a for_each like below:
resource "aws_lb_target_group" "this" {
for_each = local.combined
name = each.value.nlb_target_name
port = each.value.nlb_target_port
protocol = each.value.nlb_target_protocol
target_type = each.value.nlb_target_type
vpc_id = var.vpc_id
}
resource "aws_lb_target_group_attachment" "this" {
for_each = local.combined
target_group_arn = aws_lb_target_group.this[each.key].arn
target_id = each.value.id
port = var.target_group_attachment_port
}
The local.combined would need to account for all variations, so for 2 different EC2 ids’s the local would look like:
combined = tomap({
"group1" = {
"nlb_target_name" = "example-nlb1"
"nlb_target_port" = "3689"
"nlb_target_protocol" = "TCP"
"nlb_target_type" = "instance"
"id" = "idxxxxxxx"
}
"group2" = {
"nlb_target_name" = "example-nlb2"
"nlb_target_port" = "3689"
"nlb_target_protocol" = "TCP"
"nlb_target_type" = "instance"
"id" = "iyyyyyyy"
}
})
Any help to producing this format would be great. Thanks