Load balancing across different Elastic Beanstalk applications

Amazon Elastic Beanstalk now support for shared load balancers

11 - Sept-2020

https://aws.amazon.com/blogs/containers/amazon-elastic-beanstalk-introduces-support-shared-load-balancers/


The way I achieved this in the console for Application load balancer and elastic beanstalk is the following

  1. Create new target group (TG-App1)
  2. Attach TG-App1 to your beanstalk environments auto scale group. Now you will have both the beanstalk created target group and TG-App1 attached and both will now update with the instances.
  3. Create new application load balancer (ALB-App)
  4. Create ALB-App rules forwarding to TG-App1 (ex: PATH: /app1/* -> FORWARD: TG-App1)
  5. Update the beanstalk environment instance security group to allow traffic from ALB-App's security group on port 80. (you will have 2 port 80 rules now, 1 for ALB-App and 1 for the default beanstalk load balancer security group)

This allows you to setup dns on ALB-App ("loadbalancer.com") and forward traffic based on rules to different target groups that have instances managed by different beanstalks. Just follow the steps to create a target group for each beanstalk environment and add it to the rules on ALB-App

the result:

"loadbalancer.com/app1" -> ALB-App -> TG-App1 -> Beanstalk Environment 1 instances

"loadbalancer.com/app2" -> ALB-App -> TG-App2 -> Beanstalk Environment 2 instances


I just managed to do it, following the instructions in this article https://aws.amazon.com/blogs/devops/introducing-application-load-balancer-unlocking-and-optimizing-architectures/

the steps:

1) create a new target group

    aws elbv2 create-target-group --name <target_group_name> --protocol HTTP --port 80 --vpc-id <vpc_id> 

2) bind your target group to the autoscaling group associated to the app

    aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name <id_of_the_autoscaling_group> --target-group-arns "<new_target_group_arns>"

3) create a new rule in the main application load balancer, that routes the desired path to the right application (this can be done through the UI).