Amazon-Web-Services

使用 Terraform 在 AWS 上使用網路負載均衡器 (NLB) 創建 AutoScaling 組時出現問題

  • December 6, 2017

我正在嘗試使用 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 initterraform plan -out=plan.json一切順利,但是在 run 之後terraform apply plan.json,Terraform 花了一些時間嘗試創建 AutoScaling Group 並拋出如下內容:

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 “測試”
  • aws_autoscaling_group.asg:發生 1 個錯誤:
  • aws_autoscaling_group.asg:創建 AutoScaling 組時出錯:ValidationError:提供的負載均衡器可能無效。請確保它們存在並重試。狀態碼:400,請求 ID:cf2d4ac6-da9a-11e7-950f-050f1f0711f8

如何將目標組與 LB 關聯?為什麼提供的負載均衡器可能對 AutoScaling 組無效?

嘗試使用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"]
}

引用自:https://serverfault.com/questions/886900