Create an Application Load Balancer in AWS Cloud
Updated: Sep 25
Create an Application Load Balancer in AWS Cloud.
Objectives: 1. Learn to create application load balancer. 2. Learn to configure path-based routing, by adding a rule to forward requests to another target group based on URL.
Step 1: In AWS Console go to EC2 Service. Create 2 Linux Web Servers in 2 different availability zones with the help of our step wise blog here. The modification in script can be done as follows.
First WebServer in Availability Zone 1a: #!/bin/bash
yum -y update
yum -y install httpd
chkconfig httpd on
service httpd start
echo "<html><h1>Hello! This is your First Web Server!</h1></html>" > /var/www/html/index.html
Second WebServer in Availability Zone 1b: #!/bin/bash
yum -y update
yum -y install httpd
chkconfig httpd on
service httpd start
echo "<html><h1>Hello! This is your Second Web Server!</h1></html>" > /var/www/html/index.html

Similarly, create 2 Linux Image Server with the following script. The script can be as follows. First ImageServer in Availability Zone 1a: #!/bin/bash
yum -yupdate
yum -yinstall httpd
chkconfig httpd on
service httpd start
mkdir /var/www/html/img/
echo "<html xmlns='http://www.w3.org/1999/xhtml' >
<head><title>My First Website Home Page</title></head> <body>
<h1>Welcome to mywebsite</h1> <p>From First Images Webserver!</p> <a href='1.jpg'>Marigolds</a>
<ahref='2.jpg'>Roses</a> </body>
</html>" > /var/www/html/img/index.html
Second ImageServer in Availability Zone 1b: #!/bin/bash
yum -yupdate
yum -yinstall httpd
chkconfig httpd on
service httpd start
mkdir /var/www/html/img/
echo "<html xmlns='http://www.w3.org/1999/xhtml' >
<head><title>My Second WebsiteHome Page</title></head> <body>
<h1>Welcome to mywebsite</h1> <p>From Second Images Webserver!</p> <a href='1.jpg'>Marigolds</a>
<ahref='2.jpg'>Roses</a> </body>
</html>">/var/www/html/img/index.html

Step 2: In EC2 Service console side panel, go to Target Groups under Load Balancing. Here we have to create one target group for Web Servers. Click on Create Target Group.

Scroll down and provide Target group name as MyWebServerTG. Scroll down further to Health checks, enter Health check path as /index.html.

Under Advanced health check settings drop down, give the values as follows: Healthy threshold: 2 Unhealthy threshold: 2 Timeout: 3 Interval: 10

Scroll down and click on Next. In Register targets, register the two WebServers as targets. Click on Include as pending below.

Scroll down and click on Create target group.

Step 3: In EC2 Service console side panel, go to Target Groups under Load Balancing. Now we create a new TG for the Image Servers. Click on Create Target Group. Scroll down and provide Target group name as MyImagebServerTG. Scroll down further to Health checks, enter Health check path as /img/index.html.

Under Advanced health check settings drop down, give the values as follows: Healthy threshold: 2 Unhealthy threshold: 2 Timeout: 3 Interval: 10
Scroll down and click on Next. In Register targets, register the two ImageServers as targets. Click on Include as pending below.

Scroll down and click on Create target group.
Step 4: In EC2 Service console side panel, go to Load Balancers under Load Balancing. Click on Create Load Balancer. Select Application Load Balancer.

Provide Load Balancer Name as MyALB.

Under network mapping, scroll down to Availability Zones and select availability zones in which web servers and image servers are created. In our case we select 1a and 1b.

Under Security groups, deselect the default Security group and select the WebServerSG used while creation of above 4 EC2 servers.

Under Listeners and routing go to HTTP:80 and from Listener and from Default action open the drop-down and select the WebServerTG as target group.

Click Tags and provide Key: Name and Value: MyAppLB. Keep rest defaults as it is and Click on Create Load Balancer.
Step 5:: In EC2 Console go to Load Balancer, select the MyALB Load Balancer, scroll down and click on the Listeners Tab. Select HTTP:80 and click on Manage rules –> Add rule

In Name and tags click on Next. Click on Add condition and select Path from the dropdown. Under Path type /img/*. Then click on Confirm

Click on Next. Select MyImageServerTG from dropdown. Again click on Next.


Set Priority to 1 and click on Next. Then click on Create.

Step 6: Go back to Load Balancers. Select MyALB. Copy DNS name. Run the DNS name in new tab. Refresh the page a few times. You will observe the change in servers..


To check the application load balancing between image servers, runthe above URL with /img/ at the end.

Note: If you no longer need the Load Balancer, delete the ALB and Target Groups. Also Terminate the four instances.
Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference.
For your aws certification needs or for aws learning contact us.