获取无效索引和空元组错误

Getting invalid index and empty tuple error

我正在尝试将目标组附加到 terraform 中的负载均衡器,但出现以下错误:

resource "aws_lb_target_group" "mytargetgroup" {
  count = var.environment_acronym == "pd" ? 1 : 0
  name                    = "My Target Group"
  port                    = 80
  protocol                = "HTTP"
  vpc_id                  = var.vpc_main_id

}

    resource "aws_lb_target_group_attachment" "mytargetgroup_attachment" {
      target_group_arn = aws_lb_target_group.mytargetgroup[0].arn  --> having issues here
      target_id        = var.private_admin
      port             = 80
   }

错误:索引无效 在资源中的 lb_tg_attachments\lb_public_myloadbalancer_attach.tf 第 70 行 “aws_lb_target_group_attachment” “mytargetgroup_attachment”: 70: target_group_arn = aws_lb_target_group.mytargetgroup[0].阿恩 ├────────────────── │ aws_lb_target_group.mytargetgroup 是空元组

  The given key does not identify an element in this collection value.

我怎样才能避免这个错误。任何建议表示赞赏。

你的条件var.environment_acronym == "pd"一定是假的,所以aws_lb_target_group不存在。因此它不起作用。您稍后也必须检查它:

    resource "aws_lb_target_group_attachment" "mytargetgroup_attachment" {
      count = var.environment_acronym == "pd" ? 1 : 0

      target_group_arn = aws_lb_target_group.mytargetgroup[0].arn  --> having issues here
      target_id        = var.private_admin
      port             = 80
   }