Search code examples
amazon-web-servicescloudamazon-ecs

AWS Registering Multiple Target Groups with a ECS service


I need to create multiple target groups for an ECS service. Does anyone have an example of how I can do this via AWS CLI or API? As it is a recent functionality I have not found many examples.


Solution

  • This is a relatively new feature at ECS, I have not had the opportunity to test it in a project but just reading the documentation, it looks like pretty straight forward: Just add multiple load balancer (target groups) definitions inside the service. For example, if you're using Terraform, just add multiple load_balancer blocks:

    resource "aws_ecs_service" "my_service" {
      name            = "my_service"
      cluster         = "${aws_ecs_cluster.foo.id}"
      task_definition = "${aws_ecs_task_definition.my_task.arn}"
      ... # other arguments
    
    
      ordered_placement_strategy {
        ...
      }
    
      load_balancer {
        target_group_arn = "${aws_lb_target_group.one.arn}"
        container_name   = "my_container_name"
        container_port   = 1234
      }
    
      load_balancer {
        target_group_arn = "${aws_lb_target_group.two.arn}"
        container_name   = "my_container_name"
        container_port   = 4321
      }
    }