如何将两个目标组附加到单个 ECS 服务

How to attach Two target group against single ECS services

我正在寻找一种方法将两个目标组附加到单个 ECS 服务,在其他情况下,我的容器公开了两个端口,但我只能将一个端口映射到我的服务到 LB。

到目前为止,我能够创建一个新的侦听器和目标组,但是在创建目标组之后,我可以按照预期看到所有内容,但目标组显示 There are no targets registered to this target group

这是我的目标群体和听众配置

target_group:

resource "aws_lb_target_group" "e_admin" {
  name = "${var.env_prefix_name}-admin"
  port = 5280
  protocol = "HTTP"
  vpc_id = "${aws_vpc.VPC.id}"

  health_check {
    path = "/admin"
    healthy_threshold = 2
    unhealthy_threshold = 10
    port = 5280
    timeout = 90
    interval = 100
    matcher = "401,200"
  }
}

听众:'

resource "aws_lb_listener" "admin" {
  load_balancer_arn = "${aws_lb.admin_lb.arn}"
  port = "5280"
  protocol = "HTTP"

  default_action {
    target_group_arn = "${aws_lb_target_group.e_admin.id}"
    type = "forward"
  }
}

我的问题是如何添加 ECS 集群 Autoscaling 组或如何将 ECS 集群中的所有实例 运行 添加到此目标组?

根据https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-load-balancing.html,

There is a limit of one load balancer or target group per service.


如果要将自动缩放组附加到目标组,请使用 aws_autoscaling_attachmenthttps://www.terraform.io/docs/providers/aws/r/autoscaling_attachment.html

resource "aws_autoscaling_attachment" "asg_attachment_bar" {
  autoscaling_group_name = "${aws_autoscaling_group.your_asg.id}"
  alb_target_group_arn   = "${aws_alb_target_group.e_admin.arn}"
}

AWS recently announced 支持 ECS 服务的多个目标组。

当前未发布,2.22.0 version of the AWS provider contains support for this by adding more load_balancer blocks to the aws_ecs_service resource. Example from the acceptance tests

resource "aws_ecs_service" "with_alb" {
  name            = "example"
  cluster         = "${aws_ecs_cluster.main.id}"
  task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
  desired_count   = 1
  iam_role        = "${aws_iam_role.ecs_service.name}"

  load_balancer {
    target_group_arn = "${aws_lb_target_group.test.id}"
    container_name   = "ghost"
    container_port   = "2368"
  }

  load_balancer {
    target_group_arn = "${aws_lb_target_group.static.id}"
    container_name   = "ghost"
    container_port   = "4501"
  }

  depends_on = [
    "aws_iam_role_policy.ecs_service",
  ]
}