Search code examples
amazon-web-servicesamazon-ec2routesmicroservicesaws-application-load-balancer

AWS Load Balancer to route traffic to one target group with multiple microservices


I have ALB which listens to HTTP traffic on port 80, i have added a target-group in ALB listeners which consists of a single EC2 machine. My EC2 instance runs multiple microservices on different ports for eg. App1 runs on 8080 , App2 on 8001, App3 on 8004 so on... EC2 listens on port 80 again for any incoming requests through ALB. I want to do a path based routing for incoming traffic to EC2 to different app ports based on the path for example,

"/users" -> app on 8080 ; "/get/info" -> 8001 etc

. Is there a way to achieve it? Or any better way to do what i'm trying ? Right now i have done routing based on ip-tables to route traffic from port 80 of EC2 to single port i.e 8080, but that serves only 1 of my many microservices. How can i configure it to serve all of them?


Solution

  • This is exactly what an Application Load Balancer is designed to do.

    You can create multiple Target Groups. Each target group has:

    • A name
    • A target (eg HTTP on port 8080)
    • Health Check configuration to determine whether the target(s) are healthy

    So, you would create one Target Group for each app you are running.

    You can then associate Amazon EC2 instances with each Target Group. In your case, if everything is running on a single Amazon EC2 instance, you can associate the same instance with all target Groups.

    Then, create the Application Load Balancer (or associate the Target Groups to an existing Application Load Balancer).

    In the Application Load Balancer configuration, go to the Listeners tab and add rules that send a particular path (eg /users) to a particular target group.

    See: