How to Provision Application Load Balancer in AWS
Tags: aws, tutorial, cloud, beginners
Published: 2022-09-02 07:59:39

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9bqpietsbl5i9kwxvpp.png)
-Then navigate or type in the search bar EC2 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbwbfa7bvft657qco06u.png)
-Click on Instances

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2oh32qpik174oxd0pda1.png)

-Click on launch instance

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr74cqdd1dq6ck41qhcr.png)
-We are going to be creating two EC2 instances. Let us name our first instance “Webserver1”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vov1g9j1gh2dp34i229a.png)

-Make sure you are on the free tier

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnxwc1555obe94yozp5v.png)
-Create a “Keypair” and give it any name you like

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqos1vg4mdn6xsf00bri.png)
-Create a new Security group that allows SSH traffic

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0tytl0ybube1lkt7e10.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8roj3lw1b99hmr49mgi8.png)
-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

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctta1q1kmaz6nxxhp5gq.png)
-Then click Launch Instance
-Click view all Instances
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqoztwt3gwpbkr7fdtzo.png)

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
```
-Click Launch instance
-Click view all Instances
-We have our two instances running now

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5v22huq6mfy2wtmlfu2o.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ibkjmw9ukz6jpb87nsr9.png)
-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/286ut0wnzj14cuszlxnn.png)
-Select the created Target group, click the Actions drop-down and click Register and deregister instance/IP targets
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mf1fzpdhmk82skcs6mvb.png)
-Select the instances created and click Add to registered, then click Save.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ocrko96v9uazn0frrm4.png)
-Still on the left pane of your EC2 Instance dashboard click Load Balancers
Click Create Load Balancer

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bcatfz3k1r3rdao8bh9s.png)
-Select Application Load Balancer and click Create

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di9p9q67baynv70u4mun.png)
-Give it the name "Application-LB"
-Leave it as internet facing and IPv4

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2oeh0tcvm5ekmrnq71a.png)
-Select your VPC and all your public subnets

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qssax34ou55oqw6seg5v.png)
-Click the Security Group created with the first instance to the ALB

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wckm9ynxfiiy0h0dqef2.png)
-Under Configure routing--> select the Existing target group “WebserverTG”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/anjlvsrr4l21fmo4d9q4.png)
-Review and Create load balancer

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57g110aa47wycsc0t1jo.png)
**Great job. We are almost there**
-Go to the Security Group of each of the instances created and modify the Inbound rule

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgcf6e3ly04om43nln5l.png)
-Edit inbound rule allowing All Traffic from the Security Group of your load balancer

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4y5ay4gzdim5e3usy06.png)
-Click Save rules

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfhhativqd7rpvudmxdq.png)
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1ketld7uu4zdusnb8m1.png)
-This is what you get

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsm6mc68dv4ogrw169yk.png)
-Refresh the browser to confirm the load balancer is directing traffic to the other EC2 Instance

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tcaukov7nn2gj1yjp7x.png)
-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9s12ul3uj65cxv8vwjs.png)
That’s it folks…

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/355qzfu5sol2599t1i5u.gif)
**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.