14

I have N webapps. Each webapp is served by a different hostname in my domain and deployed to 2 instances running in AWS. In other words, I have 2N instances, divided into pairs which run N distinct webapps.

I'd like to set up a single AWS Elastic Load Balancer that will proxy a request to one of the two instances that serve it based on the Host header. Is that possible using a single ELB or I will need to deploy N ELBs, one for each pair of instances?

thesamet
  • 317
  • 1
  • 2
  • 7
  • http://stackoverflow.com/questions/27521758/multiple-domain-names-attached-to-same-load-balancer-as-alias-records – Shatiz Aug 12 '15 at 01:16

3 Answers3

19

A single ELB routes traffic to exactly one set of instances, and distributes the incoming traffic to all the instances "behind" it. It does not selectively route traffic based on any layer 7 analysis of the traffic, such as the Host: header.

You need one ELB for each set of instances. As you describe it, that's one ELB for each webapp.

If your primary purpose for running ELB is offloading the SSL using a wildcard certificate (I have one system designed like this, with dozens of apps living at many-different-domains.my-wildcard-cert-domain.com), then the instances "behind" the ELB could be running a reverse proxy such as HAProxy (or several other alternatives, like Varnish) that can make layer-7 routing decisions and then forward the traffic to the appropriate subset of machines behind them, which also allows more sophisticated load balancing and has the advantage of providing you with stats and traffic counters, aggregate and separate.

       /-- HAProxy \  /----- instances hosting app #1  
ELB ---|            >> ----- instances hosting app #2 
       \-- HAProxy /  \----- instances hosting app #n 

The intermediate ^^^^ instances can evaluate the Host: headers (among other things) and even capture the value of the session cookie in their logs for analysis.

This setup also allows me to run multiple apps on overlapping subsets of instances, where appropriate, and do a lot of other things that ELB by itself doesn't directly support. It also returns a custom "503" page in the case where an application gets overloaded or otherwise becomes unavailable, which ELB does not do on its own. I've depicted 2 proxy servers here, for no particular reason other than your mention of the number 2 in the question. My setup actually has 3, one for each availability zone in the region where this is deployed.

Michael - sqlbot
  • 21,988
  • 1
  • 57
  • 81
  • 3
    In August 2016, Amazon launched [AWS Application Load Balancer](https://aws.amazon.com/blogs/aws/new-aws-application-load-balancer/) which allows routing to different target groups based on HTTP headers. – Jens Bannmann Apr 27 '17 at 09:09
  • 2
    @JensBannmann that is true. When they released it, the only routing rules it supported was based on the path. In 2017, they added the ability to route based on the `Host:` header. This answer was correct back when I wrote it in 2014, but it looks like it's time for an update. Thanks for bringing it back to my attention. – Michael - sqlbot Apr 27 '17 at 11:50
4

As of August 2016, Amazon ELB offers two different types load balancers:

  • Classic Load Balancer routes all traffic to one set of instances without considering application protocol content. Previously, this was the only ELB type available.
  • Application Load Balancer can route traffic to different target groups according to configurable rules based on URL paths. The blog post New – AWS Application Load Balancer mentions applications consisting of several microservices as the prime use case, and gives detailed setup instructions.

For your use case, Application Load Balancer seems ideal. It is limited to 10 rules, however, so it will only work easily if your N is not larger than 10.

Jens Bannmann
  • 553
  • 1
  • 6
  • 11
0

On your list of ec2 instances, you see two instances, right? But on each instance have you many webapps? You use virtual host to deploy each one?

You need just one ELB to split the charge on your hosts. Example:

External request > ELB (based on CNAME pointer)  > EC2 Instance. 

I think that's better your give me more of your scenario and maybe i can help you.

Regards.

UPDATE

If you have differents webapps on the EC2 instances, you need to have two Elastic Load Balancers (ELB). Each one for a pair of instances. Totally, four instances