I have a set of target groups that were created through a for_each condition iterating through a locals var in the situation below:
locals {
target-ports-param = {
"port_name" = {port = "80", health_path = "/status"}
...
}
}
resource "aws_lb_target_group" "my-target-group" {
for_each = local.target-ports-param
name = each.key
target_type = "instance"
port = each.value.port
protocol = "TCP"
vpc_id = aws_vpc.my-vpc.id
#outlines path, protocol, and port of healthcheck
health_check {
protocol = "HTTP"
path = each.value.health_path
port = 80
matcher = "200"
}
}
I want to attach my instances to the target groups that were created using the above code block. The issue is that my instance count is dynamic and that the target_id parameter only accepts a string.
How would I attach all my instances to the target group?
#attach target group to instances
resource "aws_lb_target_group_attachment" "my-target-group-attach" {
for_each = local.target-ports.param
target_group_arn = aws_lb_target_group.my-target-group[each.key].arn
target_id = aws_instance.my-servers.id
port = each.value.port
}