Creating AWS Elastic Load-balancer for Multiple domain

Mahinsha Nazeer
3 min readMar 27, 2024

--

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Today, we’ll delve into the process of establishing a load balancer to efficiently distribute traffic across multiple domains hosted on distinct EC2 instances. With three domains and three EC2 nodes allotted for each domain, our objective is to configure a load balancer and implement listener rules to direct traffic from each domain to its respective EC2 instance.

To set up the target groups for the load balancer, follow these steps meticulously:

Here we have three domains and three EC2 nodes containing contents for each domain;

Domains:
------------
test1.servernine.com
test2.servernine.com
test3.servernine.com

Servers:
------------
test1.servernine
test2.servernine
test3.servernine

First, we have two create target groups for each domain

  1. Navigate to the ‘Load Balancing’ menu, where you’ll locate the ‘Target Groups’ section.
  2. Initiate the creation of a new target group by selecting ‘Create Target Group’ positioned at the top-right corner.
  3. Under the basic configuration, specify the target type by opting for ‘Instances’.
  4. In the security group option, include the designated security groups as previously specified.
  5. Ensure the creation of three distinct target groups:
Target group 1 name: 'test1'
Target group 2 name: 'test2'
Target group 3 name: 'test3'

Now add the corresponding instances to the target group:

test1.servernine > test1
test2.servernine > test2
test3.servernine > test3

To initiate the creation of a load balancer, it’s imperative to establish a default target group and configure ACM settings beforehand. It’s crucial to note that the default target group cannot be modified at a later stage. Consequently, we’re proceeding with the creation of a subdomain to facilitate SNI configuration alongside the establishment of a default target group.

default target group name: 'default'

Upon completing the creation of target groups, the subsequent step involves securing SSL certificates for the domains through ACM (Amazon Certificate Manager). We’ll utilize listener rules to efficiently route traffic from each domain to the corresponding EC2 nodes. It’s noteworthy that establishing SSL certificates before configuring the load balancer is advisable. This approach mitigates the need for rule reconfiguration post-SSL issuance. Stay tuned for an upcoming blog detailing the process of obtaining SSL certificates via ACM.

For now, we can presume we have already issued SSL for each domain. Let's create the Load balancer — here are the refined steps:

  1. Navigate to the ‘EC2’ dashboard, proceed to the ‘Load balancer’ section, and designate a name for the load balancer, such as ‘new-lb’.
  2. Within the security group configuration, include the specified security groups.
  3. Configure listeners and routing by:
Selecting HTTPS protocol on port 443 and directing it to 'default' (default target group)

In the ‘Secure listener settings’ section:

  • Opt for the Default SSL/TLS certificate sourced from ACM, specifically for the ‘default’ domain.

Maintain the remaining configurations at their default settings.

Following the addition of listener rules:

  1. Access the ‘Load balancer’ interface.
  2. Within the ‘Listener and rules’ section, locate the existing listener.
  3. Navigate to ‘Listener rules’ and select ‘Add new rule’.
  4. Proceed through the following steps to create a new rule:
    a. Assign a descriptive name to the rule.
    b. Define the if condition by specifying the host ‘header’ as mentioned below.
    c. Set the rule to forward to a designated target group.
    d. Save the rule configuration.
host header:
--------------
test1.servernine.com, www.test1.servernine.com, *.test1.servernine.com

Target group:
-------------
test1
host header:
--------------
test2.servernine.com, www.test2.servernine.com, *.test2.servernine.com

Target group:
-------------
test2
host header:
--------------
test3.servernine.com, www.test3.servernine.com, *.test3.servernine.com

Target group:
-------------
test3

Now the configuration is complete. The final step is to set up the DNS for the domains so that it points to the Load balancer. We can add the CNAMEs for the domains, you can get the endpoint from the Load balancer console.

Now the setup is complete.

--

--