Elastic Load Balancer for multiple webapps
Solution 1:
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.
Solution 2:
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.