0

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

我想在这个实例前面使用网络负载均衡器,所以我必须创建目标组和目标组附件。在这里,我必须提供 target_id(launch configuration id) 以将其附加到目标组。但是在应用脚本时会显示以下错误:

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

地形脚本

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
  }
}
4

1 回答 1

0

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

target_id(必需)目标的 ID。这是实例的实例 ID,或 ECS 容器的容器 ID。如果目标类型为 ip,请指定 IP 地址。如果目标类型是 lambda,则指定 lambda 的 arn。

如果您想让自动缩放组中的所有实例自动附加到目标组,那么您可以使用资源target_group_arns上的参数aws_autoscaling_group指定它:

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
  }
}
于 2020-04-15T11:40:32.283 回答