【发布时间】:2020-06-04 04:06:32
【问题描述】:
我有两个 target_groups - 一个用于端口 80,另一个用于 443。还有两个实例作为(该 NLB 的)成员,我需要将两个目标组附加到每个实例。所以这就是我的配置方式,要附加:
// Creates the target-group
resource "aws_lb_target_group" "nlb_target_groups" {
for_each = {
for lx in var.nlb_listeners : "${lx.protocol}:${lx.target_port}" => lx
}
name = "${var.vpc_names[var.idx]}-tgr-${each.value.target_port}"
deregistration_delay = var.deregistration_delay
port = each.value.target_port
protocol = each.value.protocol
vpc_id = var.vpc_ids[var.idx]
proxy_protocol_v2 = true
health_check {
port = each.value.health_port
protocol = each.value.protocol
interval = var.health_check_interval
healthy_threshold = var.healthy_threshold
unhealthy_threshold = var.unhealthy_threshold
}
}
// Attach the target groups to the instance(s)
resource "aws_lb_target_group_attachment" "tgr_attachment" {
for_each = {
for pair in setproduct(keys(aws_lb_target_group.nlb_target_groups), var.nlb_members.ids) : "${pair[0]}:${pair[1]}" => {
target_group = aws_lb_target_group.nlb_target_groups[pair[0]]
instance_id = pair[1]
}
}
target_group_arn = each.value.target_group.arn
target_id = each.value.instance_id
port = each.value.target_group.port
#target_id = [for tid in range(var.inst_count) : data.aws_instances.nlb_insts.ids[tid]]
}
var.nlb_listeners 的定义如下:
nlb_listeners = [
{
protocol = "TCP"
target_port = "80"
health_port = "1936"
},
{
protocol = "TCP"
target_port = "443"
health_port = "1936"
}
]
而var.elb_members.ids是这样的:
"ids" = [
"i-015604f88xxxxxx42",
"i-0e4defceexxxxxxe5",
]
但我收到 Invalid for_each 参数错误:
错误:for_each 参数无效
在 ../../modules/elb/balencer.tf 第 46 行,在资源中 “aws_lb_target_group_attachment”“tgr_attachment”:46:for_each = { 47:用于配对 设置产品(键(aws_lb_target_group.nlb_target_groups), var.elb_members.ids) : "${pair[0]}:${pair[1]}" => { 48:
target_group = aws_lb_target_group.nlb_target_groups[pair[0]] 49:
instance_id = pair[1] 50: } 51: }“for_each”值取决于不能被 在应用之前确定,因此 Terraform 无法预测有多少实例 将被创建。要解决此问题,请使用 -target 参数 首先只应用for_each所依赖的资源。
我不知道为什么它无效或者这个 for_each 无法确定值。知道我在这里做错了什么吗?严重卡在中间,非常感谢任何帮助我走向正确的方向。
-S
=== 更新:02/23 ==========
@马丁-阿特金斯,
我想我理解你所说的,但即使对于已经存在的实例,它似乎也会给我同样的错误。无论如何,这是我的aws_instance 资源:
resource "aws_instance" "inst" {
count = var.inst_count
instance_type = var.inst_type
depends_on = [aws_subnet.snets]
ami = data.aws_ami.ubuntu.id
# the VPC subnet
subnet_id = element(aws_subnet.snets.*.id, count.index)
vpc_security_group_ids = [var.sg_default[var.idx], aws_security_group.secg.id]
user_data = <<-EOF
#!/bin/bash
hostnamectl set-hostname ${var.vpc_names[var.idx]}${var.inst_role}0${count.index + 1}
# Disable apt-daily.service & wait until `apt updated` has been killed
systemctl stop apt-daily.service && systemctl kill --kill-who=all apt-daily.service
while ! (systemctl list-units --all apt-daily.service | egrep -q '(dead|failed)')
do sleep 1; done
EOF
# the public SSH key
key_name = var.key_info
tags = merge(
var.common_tags,
{ "Name" = "${var.vpc_names[var.idx]}${var.inst_role}0${count.index + 1}" }
)
}
您认为还有什么办法可以解决这个问题吗?
-S
【问题讨论】:
标签: terraform terraform-provider-aws nlb