Reputation: 193
I have two EC2 instances running websites on port 80.
Is it possible to use/configure 1 load balancer for these two instance/services? When I try to add a listener on existing load balancer, it says "You may not have duplicate load balancer ports defined".
Or should I use 2 load balancer, one for each instance?
Upvotes: 3
Views: 8348
Reputation: 12572
Load Balancers define listeners by the incoming traffic ports they listen on. When a client connection hits the load balancer on a certain port (i.e. 443 or 80), a listener forwards that request to a target group. To have a listener on port 80 forward connections to multiple instances all you need to do is create a target group containing those instances.
The most common approach for public-facing websites is to use something like Amazon Certificate Manager to create TLS certificates for the website. Then they load that certificate into their load balancer and listen on port 443 (typical port for HTTPS) and pass the connections over HTTP (typically port 80) on to the target groups. This is commonly referred to as TLS or SSL Termination.
If the instances are hosting different websites but you want to route based on the hostname submitted (i.e. example.com and example1.com) then you can create a separate target group for each website and use an Application Load Balancer (ALB) to route based on the hostname supplied by the requester.
This can be done after you have created the ALB by going to the ALB subconsole and modifying the listener rules similar to the screenshots below.
To serve a separate certificate for each host you'll also need to add those certificates to the listener as I wrote in my blog post outlining the feature.
Upvotes: 13