每个NLB我有多个target_group,我需要将多个实例附加到每个target_group。 target_id
是aws_lb_target_group_attachment
资源中的一个字符串,我看不到任何简单的方法来实现。这就是我正在做的atm:
variable "nlb_listeners" {
default = [
{
protocol = "TCP"
target_port = "80"
health_port = "1936"
},
{
protocol = "TCP"
target_port = "443"
health_port = "1936"
}
]
}
// Get the instance ids of the NLB members
data "aws_instances" "nlb_insts" {
instance_tags = {
Name = "${var.vpc_names[var.idx]}${var.inst_role}0*"
}
instance_state_names = ["running", "stopped"]
}
// EC2 instances
resource "aws_instance" "insts" {
count = var.inst_count
instance_type = var.inst_type
.......
}
// Creates the target-group
resource "aws_lb_target_group" "nlb_target_groups" {
count = length(var.nlb_listeners)
name = "nlb-tgr-${lookup(var.nlb_listeners[count.index], "target_port")}"
deregistration_delay = var.deregistration_delay
port = lookup(var.nlb_listeners[count.index], "target_port")
protocol = lookup(var.nlb_listeners[count.index], "protocol")
vpc_id = var.vpc_ids[var.idx]
health_check {
port = lookup(var.nlb_listeners[count.index], "health_port")
protocol = lookup(var.nlb_listeners[count.index], "protocol")
interval = var.health_check_interval
unhealthy_threshold = var.unhealthy_threshold
healthy_threshold = var.healthy_threshold
}
}
// Attach the target groups to the instance(s)
resource "aws_lb_target_group_attachment" "tgr_attachment" {
count = length(var.nlb_listeners)
target_group_arn = element(aws_lb_target_group.nlb_target_groups.*.arn, count.index)
target_id = [for sc in range(var.inst_count) : data.aws_instances.nlb_insts.ids[sc]]
port = lookup(var.nlb_listeners[count.index], "target_port")
}
所以,我收到此错误:
错误:属性值类型错误
资源中../../modules/elb/balencer.tf第31行的“ aws_lb_target_group_attachment”“ tgr_attachment”:31:target_id = [对于范围(2)中的sc:data.aws_instances.nlb_insts.ids [sc]]
属性“ target_id”的值不合适:必须输入字符串。
任何人都知道如何实现这一目标吗?