如何针对单个ECS服务附加两个目标组

时间:2019-07-19 15:05:19

标签: terraform amazon-ecs terraform-provider-aws

我正在寻找一种针对单个ECS服务附加两个目标组的方法,在另一个容器中,我公开了两个端口,但是我只能将一个针对我的服务的端口映射到LB。

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

enter image description here 这是我的目标组和侦听器配置

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群集自动扩展组或如何将ECS群集中运行的所有实例添加到该目标组?

预先感谢

2 个答案:

答案 0 :(得分:2)

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

当前未发布的2.22.0 version of the AWS provider通过向load_balancer资源添加更多aws_ecs_service块来包含对此的支持。 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",
  ]
}

答案 1 :(得分:1)

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

  

每个服务最多只能有一个负载均衡器或目标组。


如果要将自动伸缩组附加到目标组,请使用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}"
}