DEV Community

Cover image for How to Provision Application Load Balancer in AWS
Opeyemi Jokanola
Opeyemi Jokanola

Posted on

How to Provision Application Load Balancer in AWS

When building on the cloud, one of the advantages of cloud computing is High availability which means that people can always access your application about 99.99% of the time without worrying about downtime of your application/website. The tenets of High availability dictate that you run your application/system in at least 2 Availability Zones.
Load balancers are servers that forward internet traffic to multiple servers (EC2 Instances) downstream. If one of your servers is down or unhealthy, the load balancer helps to direct traffic to the available server to ensure no downtime. Load balancers ensure organisations can maintain the performance and availability of their applications which helps them stay a step ahead of the competition. Load balancers are super helpful for sudden traffic spikes where application load balancers effectively split network load to the cloud, boosting availability and scalability.

Why do we use a load balancer
-Load balancer helps to spread the load across multiple downstream instances
-Expose a single point of access (DNS) to your application
-Seamlessly handle failures of downstream instances
-Do regular health checks on your instances
-Provide SSL termination (HTTPS) for your websites
-Enforce stickiness with cookies
-Separate public traffic from private traffic
-High availability across zones

Types of load balancer
There are 4 types of Load balancers in AWS
1-Classic Load Balancer (v1 - old generation) – 2009 – CLB (soon to be phased out)
Supports HTTP, HTTPS, TCP, SSL (secure TCP)
2-Application Load Balancer (v2 - new generation) – 2016 – ALB
Supports HTTP, HTTPS, WebSocket
3-Network Load Balancer (v2 - new generation) – 2017 – NLB
Support TCP, TLS (secure TCP), UDP
4-Gateway Load Balancer – 2020 – GWLB
Operates at layer 3 (Network layer) – IP Protocol

We will be focusing on Application Load Balancer today
Application load balancer is Layer 7 (HTTP) which balances the load to multiple HTTP applications across target groups. The application load balancer handles load balancing to multiple applications on the same machine (e.g containers). It supports HTTP/2, WebSocket and redirects traffic from HTTP to HTTPS.
Application load balancer enable routing tables to different/multiple target groups. They are a great fit for microservices & container-based applications (e.g Docker & Amazon ECS). It has a port mapping feature to redirect to a dynamic port in ECS. Application load balancers usually have a fixed hostname (e.g Application-LB-506567568.us-east-1.elb.amazonaws.com) and the application servers don’t see the IP of the client directly.

Now let’s get our hands dirty in provisioning an Application Load Balancer

-Login to your AWS Console
Image description
-Then navigate or type in the search bar EC2
Image description
-Click on Instances

Image description

-Click on launch instance

Image description
-We are going to be creating two EC2 instances. Let us name our first instance “Webserver1”

Image description

-Make sure you are on the free tier

Image description
-Create a “Keypair” and give it any name you like

Image description
-Create a new Security group that allows SSH traffic

Image description

Image description
-Expand Advanced details, scroll down to user data and input the script below:

#!/bin/bash
# Please make sure to launch Amazon Linux 2
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from Webserver1</h1>" > /var/www/html/index.html
echo "<h1>Healthy</h1>" > /var/www/html/health.html

Enter fullscreen mode Exit fullscreen mode

Image description
-Then click Launch Instance
-Click view all Instances
Image description

As I mentioned earlier, we are creating two EC2 Instances. Follow the same process to create another EC2 instance with the name Webserver2.
-A keypair and a public IP address.
-Security group that allows SSH traffic
-Expand Advanced details, scroll down to user data and input the script below:

#!/bin/bash
# Please make sure to launch Amazon Linux 2
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from Webserver2</h1>" > /var/www/html/index.html
echo "<h1>Healthy</h1>" > /var/www/html/health.html
Enter fullscreen mode Exit fullscreen mode

-Click Launch instance
-Click view all Instances
-We have our two instances running now

Image description

Great you have made it thus far. Next for us is to create a Target Group.
-Look at the left pane on your EC2 Instance dashboard and scroll down to Target group, then click Create Target group

Image description
-Give it the name "WebserverTG"
-Leave everything as default
-Ensure to select the same VPC used when creating the Instances
-Under Health checks path paste: "health.html"
-Then click Create
Image description
-Select the created Target group, click the Actions drop-down and click Register and deregister instance/IP targets
Image description
-Select the instances created and click Add to registered, then click Save.

Image description
-Still on the left pane of your EC2 Instance dashboard click Load Balancers
Click Create Load Balancer

Image description
-Select Application Load Balancer and click Create

Image description
-Give it the name "Application-LB"
-Leave it as internet facing and IPv4

Image description
-Select your VPC and all your public subnets

Image description
-Click the Security Group created with the first instance to the ALB

Image description
-Under Configure routing--> select the Existing target group “WebserverTG”

Image description
-Review and Create load balancer

Image description
Great job. We are almost there
-Go to the Security Group of each of the instances created and modify the Inbound rule

Image description
-Edit inbound rule allowing All Traffic from the Security Group of your load balancer

Image description
-Click Save rules

Image description
Alas, we got to the finish line.Yipeee!!!
-Navigate on your Instance dashboard to the Load balancer. Copy the DNS name and paste in a browser.

Image description
-This is what you get

Image description
-Refresh the browser to confirm the load balancer is directing traffic to the other EC2 Instance

Image description
-On the web browser where you pasted the DNS name of the Application LB, add “/health.html” at the end of the URL to confirm if the EC2 Instance is Healthy

Image description
That’s it folks…

Image description
Do not forget to clean up your resources (i.e delete everything you have provisioned)

Please drop a comment in the chat box and let me know if you find this helpful.

Top comments (0)