我有三个名为valid、jsc和test的服务,每个服务在3个区域中有3个实例。现在,我必须为每个服务创建一个目标组,并将实例附加到该目标组。现在我不知道如何将端口80443与服务名称组合起来创建目标组
variable "service-names" {
type = list
default = ["valid","jsc","test"]
}
variable "net-lb-ports" {
type = map(number)
default = {
TCP = 80
TCP = 443
}
}
现在,我必须将这个服务名称变量(列表(和净lb端口(映射(结合起来,以创建目标组
我可以做下面的事情,但只有1个端口,这也通过hartcode的值
resource "aws_lb_target_group" "ecom-nlb-tgp" {
for_each = toset(var.service-names)
name = "${each.value}-nlbtgp"
port = 80
protocol = "TCP"
vpc_id = aws_vpc.ecom-vpc.id
target_type = "instance"
deregistration_delay = 90
health_check {
interval = 30
port = 80
protocol = "TCP"
healthy_threshold = 3
unhealthy_threshold = 3
}
tags = {
"Name" = "${each.value}-nlb-tgp"
}
因此,我总共需要6个目标组,其中3个(服务名称(*2个端口(80443(
请引导我
我如下所示更改了变量,并能够创建目标组
variable net-lb-ports {
type = map
default = {
port1 = {
port = 80
protocol = "TCP"
}
port2 = {
port = 443
protocol = "TCP"
}
}
}
locals {
merged_lbport_svc = flatten([
for s in var.service-names : [
for v in var.net-lb-ports : {
service = s
port = v.port
protocol = v.protocol
}
]
])
}
resource "aws_lb_target_group" "ecom-nlb-tgp" {
for_each = {for idx,svc in local.merged_lbport_svc : "${svc.service}-${svc.port}-${svc.protocol}" => svc}
#for_each = {for idx,svc in local.merged_lbport_svc : idx => svc}
name = "ecom-${each.value.service}-${each.value.port}-${each.value.protocol}-nlbtgp"
port = each.value.port
protocol = each.value.protocol
vpc_id = aws_vpc.ecom-vpc.id
target_type = "instance"
deregistration_delay = 90
health_check {
interval = 30
port = each.value.port
protocol = each.value.protocol
healthy_threshold = 3
unhealthy_threshold = 3
}
tags = {
"Name" = "ecom-${each.value.service}-${each.value.port}-${each.value.protocol}-nlbtgp"
}
}