每个 NLB 有多个target_groups,我需要将多个实例附加到每个target_group。target_id
是资源aws_lb_target_group_attachment
字符串,我看不到任何简单的方法可以实现这一点。这就是我正在做的 atm:
vars.tf
variable "nlb_listeners" {
default = [
{
protocol = "TCP"
target_port = "80"
health_port = "1936"
},
{
protocol = "TCP"
target_port = "443"
health_port = "1936"
}
]
}
实例 .tf
// 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
.......
}
balancer.tf
// 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"的值不合适:需要字符串。
有人知道我如何实现这一目标吗?
快速查看您的代码,我会说这是给您带来的错误:
data.aws_instances.nlb_insts.ids[sc]
应该是:
data.aws_instances.nlb_insts[sc].ids
索引继续资源,然后你从你的资源中获取 id,在你的例子中,你试图获取所有资源的所有 id,然后获取它的索引,这不是这样做的方法!