无法将目标 ID(启动配置)附加到 Terraform 中的网络负载均衡器目标组

Not able to attach Target id(launch configurtion) to the target group of network load balancer in Terraform

我正在配置一个 Terraform 脚本,用于为所有实例创建具有自动缩放组的 HA rabbitmq 设置。为了创建自动缩放组,我正在创建将创建自动缩放实例的启动配置。

我想在此实例前面使用网络负载平衡器,因此我必须创建目标组和目标组附件。在这里,我必须提供 target_id(启动配置 ID)以将其附加到目标组。但是在应用脚本时它显示以下错误:

Error registering targets with target group: ValidationError: Instance ID 'rabbit' is not valid status code: 400, request id: 1cad37a8-b1da-416a-bc11-f50ae6b83cd2

Terraform 脚本

resource "aws_lb" "rabbit" {
  name = "${local.cluster_name}-lb"
  load_balancer_type = "network"
  internal = "false"
  subnets = aws_subnet.subnet.*.id
  enable_cross_zone_load_balancing = "true"
  tags = {
    Name = "${local.cluster_name}-lb"
  }
}

resource "aws_lb_listener" "http" {
    load_balancer_arn = aws_lb.rabbit.arn
    protocol = "TCP"
    port = "80"

    default_action {
        type = "forward"
        target_group_arn = aws_lb_target_group.TCP80.arn
    }
}

resource "aws_lb_target_group" "TCP80" {
  name = "${local.cluster_name}-TCP80"
  vpc_id = aws_vpc.vpc.id
  target_type = "instance"

  protocol = "TCP"
  port = "80"

  health_check {
    protocol = "TCP"
    port     = 80

    # NLBs required to use same healthy and unhealthy thresholds
    healthy_threshold   = 3
    unhealthy_threshold = 3

    # Interval between health checks required to be 10 or 30
    interval = 10
  }
}

resource "aws_lb_target_group_attachment" "TCP80" {
  count = var.controller_count
  target_group_arn = aws_lb_target_group.TCP80.arn
  target_id        = aws_launch_configuration.rabbit.id
  port             = 80
}

resource "aws_launch_configuration" "rabbit" {
  name = "rabbit"
  image_id    = data.aws_ami.ubuntu.id
  instance_type = var.instance_type
  key_name    = "key_name"

  security_groups = [
      aws_security_group.rabbit-nodes.id,
  ]
}

resource "aws_autoscaling_group" "rabbit-node" {
  #name = "${var.name}-${var.environment_tag}-"
  name ="rabbit"
  #count = var.instance_count
  launch_configuration = aws_launch_configuration.rabbit.name
  vpc_zone_identifier  = aws_subnet.subnet.*.id
  min_size             = var.min_size
  max_size             = var.max_size
  desired_capacity     = var.desired_size
  termination_policies = ["OldestLaunchConfiguration", "Default"]
  #load_balancers       = ["${aws_lb.rabbit}"]

  health_check_type         = "EC2"
  health_check_grace_period = 300

  lifecycle {
    create_before_destroy = true
  }
}

aws_lb_target_group_attachment 资源用于将现有 EC2 实例、ECS 任务或 Lambda 函数连接到负载均衡器目标组:

target_id (Required) The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container. If the target type is ip, specify an IP address. If the target type is lambda, specify the arn of lambda.

如果您希望自动扩展组中的所有实例都自动附加到目标组,那么您可以使用 target_group_arns parameter on the aws_autoscaling_group resource:

进行指定
resource "aws_autoscaling_group" "rabbit-node" {
  #name = "${var.name}-${var.environment_tag}-"
  name ="rabbit"
  #count = var.instance_count
  launch_configuration = aws_launch_configuration.rabbit.name
  vpc_zone_identifier  = aws_subnet.subnet.*.id
  min_size             = var.min_size
  max_size             = var.max_size
  desired_capacity     = var.desired_size
  termination_policies = ["OldestLaunchConfiguration", "Default"]

  target_group_arns    = [aws_lb_target_group.TCP80.arn]

  health_check_type         = "EC2"
  health_check_grace_period = 300

  lifecycle {
    create_before_destroy = true
  }
}