使用 Terraform 在 AWS 上使用网络负载均衡器 (NLB) 创建 AutoScaling 组时出现问题

使用 Terraform 在 AWS 上使用网络负载均衡器 (NLB) 创建 AutoScaling 组时出现问题

我正在尝试使用 Terraform v0.11.0 创建 na NLB(我的应用程序不使用 HTTP,因此无法使用 ALB)。查看 Hashcorp 文档,我可以创建以下代码:

resource "aws_lb" "lb" {
  name = "test"
  internal = false
  enable_deletion_protection = true
  load_balancer_type = "network"
  ip_address_type = "ipv4"
  subnet_mapping {
    subnet_id = "${data.aws_subnet.sn-app-1.id}"
    allocation_id = "${aws_eip.eip-1.id}"
  }
  subnet_mapping {
    subnet_id = "${data.aws_subnet.sn-app-2.id}"
    allocation_id = "${aws_eip.eip-2.id}"
  }
}

resource "aws_lb_target_group" "lbtg" {
  name     = "test"
  port     = "8080"
  protocol = "TCP"
  vpc_id   = "${data.aws_vpc.vpc.id}"
  deregistration_delay = "300"
  health_check {
    interval = "300"
    port = "8080"
    protocol = "TCP"
    timeout = "10"
    healthy_threshold = "10" 
    unhealthy_threshold= "10" 
  }
}

resource "aws_lb_listener" "front_end" {
  load_balancer_arn = "${aws_lb.lb.arn}"
  port              = "8080"
  protocol          = "TCP"
  default_action {
    target_group_arn = "${aws_lb_target_group.lbtg.arn}"
    type             = "forward"
  }
}

resource "aws_autoscaling_group" "asg" {
  name  = "test"
  vpc_zone_identifier = ["${data.aws_subnet.sn-app-1.id}","${data.aws_subnet.sn-app-2.id}"]
  min_size  = 1
  desired_capacity  = 1
  max_size  = 3
  launch_configuration = "${aws_launch_configuration.lc.name}"
  load_balancers  = ["${aws_lb.lb.name}"]
  default_cooldown= 180
  health_check_grace_period = 180
  termination_policies = ["ClosestToNextInstanceHour", "NewestInstance"]
}

我运行terraform init并且terraform plan -out=plan.json一切顺利,但是运行后terraform apply plan.json,Terraform 花费一些时间尝试创建 AutoScaling 组并抛出如下内容:

aws_ecs_service.ecss:发生 1 个错误:

  • aws_ecs_service.ecss:InvalidParameterException:具有 targetGroupArn arn:aws:elasticloadbalancing:us-east-1:xxxxxx:targetgroup/test/xxxxxx 的目标组没有关联的负载均衡器。状态代码:400,请求 ID:b2565334-da9a-11e7-ab5a-8f0bfc9ecd99“test”

  • aws_autoscaling_group.asg:发生 1 个错误:

  • aws_autoscaling_group.asg:创建 AutoScaling 组时出错:ValidationError:提供的负载均衡器可能无效。请确保它们存在,然后重试。状态代码:400,请求 ID:cf2d4ac6-da9a-11e7-950f-050f1f0711f8

如何将目标组与 LB 关联?为什么提供的负载均衡器可能对 AutoScaling 组无效?

答案1

尝试使用target_group_arnsASG 上的选项。

resource "aws_autoscaling_group" "asg" {
    name  = "test"
    vpc_zone_identifier = ["${data.aws_subnet.sn-app-1.id}","${data.aws_subnet.sn-app-2.id}"]
    min_size  = 1
    desired_capacity  = 1
    max_size  = 3
    launch_configuration = "${aws_launch_configuration.lc.name}"
    target_group_arns = ["${aws_lb_target_group.lbtg.arn}"]
    default_cooldown= 180
    health_check_grace_period = 180
    termination_policies = ["ClosestToNextInstanceHour", "NewestInstance"]
}

相关内容