title: How to add a second registered target (with a different port) to an alb target group pointing at a task definition
body: <p>I started with this shippable tutorial as a base for creating my AWS environment: <a href="http://blog.shippable.com/setup-a-container-cluster-on-aws-with-terraform-part-2-provision-a-cluster" rel="nofollow noreferrer">http://blog.shippable.com/setup-a-container-cluster-on-aws-with-terraform-part-2-provision-a-cluster</a></p>
  
  <p>Right now I have 2 services running 2 containers each on 2 EC2 instances behind 2 load balancers (1 for each service). I am trying to add a second registered target with a different port to the target group, and point it at one of my containers. The listener was easy to add to the ALB, but I can't seem to figure out how to add a second target to dynamically point to the instance running my service.</p>
  
  <p>In the UI, I can only manually point it to an AWS Instance, not have it dynamically point to whichever instance is running my service. </p>
  
  <p>The following looks like it should work, but since my containers are created within tasks, I don't seem to have access to the container id to point the target_ip at, and it can't be pointed to a service.</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>Here's some more terraform code for context:</p>
  
  <pre><code>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}"
    }
  }
  </code></pre>