14
votes

In my AWS environment there are some load balanced / autoscaled Elastic Beanstalk applications.

I would like to have a load balancer in front of them, so any request to http://loadbalancer.com/app1 is routed to the first Elastic Beanstalk app, http://loadbalancer.com/app2 to the second and so on.

I tried to set up an application load balancer with different listeners routing to different target groups. Unfortunately my solution is not ideal, because the target groups are bound to a fixed set of EC2 instances, while I want them to be associated to an environment where instances are created or destroyed on demand

I haven't still found a way of binding an application load balancer's listener to an auto scaling group.

Is there a way of achieving what I want?

3
AWS is considering adding this feature. See: github.com/aws/elastic-beanstalk-roadmap/issues/40Benoit Blanchon

3 Answers

15
votes

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).

5
votes

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