I am trying to create a target group and attach it to 2 instances. I was able to achieve this by using the below code.
locals {
app_servers = {
"server1" = "${aws_instance.server1.id}",
"server2" = "${aws_instance.server2.id}"
}
}
resource "aws_lb_target_group" "internal" {
name = "internal-tg"
port = 9550
protocol = "HTTPS"
vpc_id = aws_vpc.main.id
protocol_version = "HTTP1"
health_check {
healthy_threshold = 5
interval = 15
protocol = "HTTPS"
unhealthy_threshold = 2
timeout = 5
matcher = "200"
path = "/login"
}
}
resource "aws_lb_target_group_attachment" "internal" {
for_each = local.app_servers
target_group_arn = aws_lb_target_group.internal.arn
port = aws_lb_target_group.internal.port
target_id = each.value
}
Now I want to add many target groups and attach it to the same 2 instances. The properties that will change with each target group are name, port and matcher. I tried adding another variable as a map under locals like below
locals {
app_servers = {
"server1" = "${aws_instance.server1.id}",
"server2" = "${aws_instance.server2.id}"
}
target_groups = {
"internal" = {
port = 9550
matcher = "200"
},
"dev1" = {
port = 9152
matcher = "302"
},
"sso" = {
port = 9154
matcher = "302"
},
"terra-test" = {
port = 9360
matcher = "200"
}
}
}
This doesn't seem to work. Need some guidance on how to achieve this. Thanks