Terraform aws_lb_目标_组_附件:将多个实例附加到每个目标_组

Terraform aws_lb_目标_组_附件:将多个实例附加到每个目标_组,terraform,terraform-provider-aws,Terraform,Terraform Provider Aws,每个NLB有多个目标组,我需要将多个实例附加到每个目标组。target\u id是aws\u lb\u target\u group\u附件资源中的一个字符串,我看不到任何简单的方法来实现这一点。这就是我正在做的: vars.tf 实例.tf 平衡器 所以,我得到了这个错误: 错误:属性值类型不正确 在资源中的.././modules/elb/balencer.tf第31行 “aws_lb_目标_组_附件”“tgr_附件”:31:目标id= [对于范围(2)中的sc:data.aws_insta

每个NLB有多个目标组,我需要将多个实例附加到每个目标组。
target\u id
aws\u lb\u target\u group\u附件
资源中的一个字符串,我看不到任何简单的方法来实现这一点。这就是我正在做的:

vars.tf 实例.tf 平衡器 所以,我得到了这个错误:

错误:属性值类型不正确

在资源中的.././modules/elb/balencer.tf第31行 “aws_lb_目标_组_附件”“tgr_附件”:31:目标id= [对于范围(2)中的sc:data.aws_instances.nlb_insts.ids[sc]]

属性“target_id”的值不正确:需要字符串


有人知道我该如何做到这一点吗?

有人提出建议/评论吗?嘿,MacUsers,你必须为每个目标资源创建一个附件,你正在传递一个列表,而“Target_id”需要一个字符串。为了实现您想要的,您需要创建一个结构,您可以使用
对每个
进行迭代。如果你认为这符合你的情况,我可以指导你。
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")
}