如何将第二个已注册目标(具有不同的端口)添加到指向任务定义的alb目标组

时间:2019-01-21 14:32:43

标签: terraform amazon-ecs

我从此可运输教程作为创建我的AWS环境的基础开始:http://blog.shippable.com/setup-a-container-cluster-on-aws-with-terraform-part-2-provision-a-cluster

现在,我有2个服务在2个负载均衡器后面的2个EC2实例上运行2个容器(每个服务1个)。我试图将另一个具有不同端口的注册目标添加到目标组,并将其指向我的一个容器。侦听器很容易添加到ALB中,但是我似乎无法弄清楚如何添加第二个目标来动态指向运行我的服务的实例。

在UI中,我只能将其手动指向一个AWS实例,而不能使其动态指向正在运行我的服务的任何实例。

以下内容看起来应该可以工作,但是由于我的容器是在任务内创建的,因此我似乎无权访问容器id来将target_ip指向该容器,并且无法将其指向服务。< / p>

resource "aws_alb_target_group_attachment" "test" {
  target_group_arn = "${aws_alb_target_group.ecs-target-group.arn}"
  target_id        = "${aws_ecs_task_definition.test.id}"
  port             = 5000
}

还有一些用于上下文的terraform代码:

resource "aws_alb" "ecs-load-balancer" {
  name                = "ecs-load-balancer"
  security_groups     = ["${aws_security_group.test_public_sg.id}"]
  subnets             = ["${aws_subnet.test_public_sn_01.id}", "${aws_subnet.test_public_sn_02.id}"]

  tags {
    Name = "ecs-load-balancer"
  }
}

resource "aws_alb_target_group" "ecs-target-group" {
  name                = "ecs-target-group"
  port                = "80"
  protocol            = "HTTP"
  vpc_id              = "${aws_vpc.test_vpc.id}"

  health_check {
      healthy_threshold   = "5"
      unhealthy_threshold = "2"
      interval            = "30"
      matcher             = "200"
      path                = "/"
      port                = "traffic-port"
      protocol            = "HTTP"
      timeout             = "5"
  }

  tags {
    Name = "ecs-target-group"
  }
}

resource "aws_alb_listener" "alb-listener" {
  load_balancer_arn = "${aws_alb.ecs-load-balancer.arn}"
  port              = "80"
  protocol          = "HTTP"

  default_action {
      target_group_arn = "${aws_alb_target_group.ecs-target-group.arn}"
      type             = "forward"
  }
}

resource "aws_alb_listener" "alb-listener-vemcoio" {
  load_balancer_arn = "${aws_alb.ecs-load-balancer.arn}"
  port              = "5000"
  protocol          = "HTTP"

  default_action {
    target_group_arn = "${aws_alb_target_group.ecs-target-group.arn}"
    type             = "forward"
  }
}

resource "aws_ecs_task_definition" "test" {
  family                = "test"
  container_definitions = "${data.template_file.test.rendered}"
}

data "template_file" "test" {
  depends_on = ["aws_instance.mongodb_one"]
  template = "${file("task-definitions/test.json")}"

  vars {
    mongo_ip = "${aws_instance.mongodb_one.private_ip}"
  }
}

1 个答案:

答案 0 :(得分:0)

您不需要使用资源"aws_alb_target_group_attachment"将目标组附加到容器,任务或实例。

您可以使用以下代码。它将自动处理ecs任务的动态端口。

检查以下代码的最后四行。

resource "aws_lb_target_group" "rc" {
  name       = "rc"
  port       = 80
  protocol   = "HTTP"
  vpc_id     = "${var.vpc_id}"
}
resource "aws_lb_listener_rule" "rc" {
  listener_arn = "${var.listener_arn}"
  priority     = 100

  action {
    type             = "forward"
    target_group_arn = "${aws_lb_target_group.rc.arn}"
  }

  condition {
    field  = "host-header"
    values = ["www.domain.com"]
  }
}
resource "aws_ecs_service" "rc" {
  name    = "${var.name}"
  cluster = "${var.cluster}"
  task_definition = "${aws_ecs_task_definition.rc.arn}"
  desired_count   = "${var.desired_count}"

  health_check_grace_period_seconds = "${var.health_grace_period}"
  deployment_maximum_percent         = "${var.deployment_maximum_percent}"
  deployment_minimum_healthy_percent = "${var.deployment_minimum_healthy_percent}"

  load_balancer {
    target_group_arn = "${aws_lb_target_group.rc.arn}"
    container_name   = "test"
    container_port   = 5000
  }
}