如何使用 terraform 添加多个 aws 目标组到多个自动缩放组

how to add adding multiple aws target groups to multiple autoscaling groups using terraform

我正在创建 4 个目标组和 4 个 nlb,计数基于屏幕录像机的数量 (01-04)。还有 3 个 ASG,每个可用区一个。我想为每个 ASG 添加 4 个目标组。 例如:fw_asg_az1 应该附加 4 个目标组,fw_asg_az2 和 fw_asg_az3 需要附加相同的目标组。

将来如果我在我的变量中添加 server05 并重新 运行 terraform,新创建的目标组应该添加到所有三个 ASG 中

我的变量 main.tf

screenrecorders = [
      "server01",
      "server02",
      "server03",
      "server04"  
  ]

variable.tf

variable "screenrecorders" {
  description = "List of screen recorders"
  type = list(string)
}

main.tf

resource "aws_autoscaling_group" "fw_asg_az1" {
  name = "${var.deployment_prefix}-asg-${var.az1}"
  availability_zones = [var.az1]
  health_check_grace_period = 300
  health_check_type         = "ELB"
  default_cooldown          = 600
  launch_template {
    id      = aws_launch_template.fw_launch_template_az1.id
    version = "$Latest"
  }
  min_size = 1
  max_size = 5
  tag {
    key                 = "Name"
    value               = "${var.deployment_prefix}-${var.az1}"
    propagate_at_launch = true
  }
  target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
  depends_on = [aws_lb_target_group.tg_scr_vr]   
}

resource "aws_autoscaling_group" "fw_asg_az2" {
  name = "${var.deployment_prefix}-asg-${var.az2}"
  availability_zones = [var.az2]
  health_check_grace_period = 300
  health_check_type         = "ELB"
  default_cooldown          = 600
  launch_template {
    id      = aws_launch_template.fw_launch_template_az2.id
    version = "$Latest"
  }
  min_size = 1
  max_size = 5
  tag {
    key                 = "Name"
    value               = "${var.deployment_prefix}-${var.az2}"
    propagate_at_launch = true
  } 
  target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
  depends_on = [aws_lb_target_group.tg_scr_vr]   
}

resource "aws_autoscaling_group" "fw_asg_az3" {
  name = "${var.deployment_prefix}-asg-${var.az3}"
  availability_zones = [var.az3]
  #availability_zones = [data.aws_availability_zones.all.names]
  health_check_grace_period = 300
  health_check_type         = "ELB"
  default_cooldown          = 600
  launch_template {
    id      = aws_launch_template.fw_launch_template_az3.id
    version = "$Latest"
  }
  min_size = 1
  max_size = 5
  tag {
    key                 = "Name"
    value               = "${var.deployment_prefix}-${var.az3}"
    propagate_at_launch = true
  }
  target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
  depends_on = [aws_lb_target_group.tg_scr_vr]
  
}

###############################################################################
#                              ELB Target Groups
###############################################################################
resource "aws_lb_target_group" "tg_scr_vr" {
  count       = length(var.screenrecorders)
  name        = "tg-${var.screenrecorders[count.index]}"
  target_type = "instance"
  protocol    = "TCP"
  port        = "80"
  vpc_id      = var.vpc_id
  health_check {
    protocol = "TCP"
    port     = "traffic-port"
    

  }
}

###############################################################################
#                              ELB
###############################################################################
resource "aws_lb" "lb_scr_vr" {
  count              = length(var.screenrecorders)
  name               = "lb-${var.screenrecorders[count.index]}"
  load_balancer_type = "network"
  internal           = false
  ip_address_type    = "ipv4"
  subnet_mapping {
    subnet_id = var.az1_fw_public_subnet_id
  }
  subnet_mapping {
    subnet_id = var.az2_fw_public_subnet_id
  }
  subnet_mapping {
    subnet_id = var.az3_fw_public_subnet_id
  }
}

我收到错误

Error: Missing resource instance key

  on .terraform/modules/fw/main.tf line 147, in resource "aws_autoscaling_group" "fw_asg_az1":
 147:   target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]

Because aws_lb_target_group.tg_scr_vr has "count" set, its attributes must
be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    aws_lb_target_group.tg_scr_vr[count.index]

我按照错误中的提示进行了以下尝试,仍然无效

aws_lb_target_group.tg_scr_vr[count.index]

我是 Terraform 的新手,仍在学习中,非常感谢来自 Whosebug 社区的任何帮助。

而不是

 target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]

应该是:

 target_group_arns = aws_lb_target_group.tg_scr_vr[*].arn

如果您想将所有四个 TF 与一个 ASG 相关联。