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?


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.

  • 3
    In August 2016, Amazon launched AWS Application Load Balancer which allows routing to different target groups based on HTTP headers. – Jens Bannmann Apr 27 '17 at 9: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

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.


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.



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

  • On my instance list I see 2N apps. There are N webapps, two instances for each. I'll edit the question to clarify this. – thesamet Feb 7 '14 at 18:02
  • Updated response. – Junior Rocha Feb 7 '14 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.