Hands-on: AWS Elastic Load Balancer
Tags: aws, application, loadbalancer, tutorial
Published: 2023-07-07 17:25:28

Following up from the previous article, let's dive into configuring a Application Load Balancer in AWS.


![diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9puew6fqdigxthchy1g.jpg)
For this tutorial, we will use two web servers (using EC2 instances) and we will configure them to use a single endpoint, which is the Application load balancer.

**Pre-requisite**
AWS account
Some knowledge of EC2 instance.

Login into your AWS console, navigate to the EC2 dashboard and let's spin up EC2 instance.
Input the following values
- Number of instance = 2
- OS image = ubuntu
- AMI = Ubuntu Server 20.04 LTS
- Instance type = t2.micro
- Key pair = select your key
- Security groups = Allow SSH traffic

![instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2jw821wzwwo7ih999o0.png)
Select _**Advanced details**_ and scroll down to user data and paste the follow script. 

```
#!/bin/bash
sudo apt update
sudo apt install nginx wget unzip -y
wget https://www.tooplate.com/zip-templates/2133_moso_interior.zip
unzip 2133_moso_interior.zip
sudo mv 2133_moso_interior/* /var/www/html
sudo rm -rf 2*
```
This script simply bootstrap the instances at startup. It updates the ubuntu package, installs three dependencies nginx server to server the website, wget to download a simple static website template, unzips the folder, copies the files to the default folder Nginx servers content from and finally deletes the folders and file from the home directory.

![EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lei1ihbj7u7d2azctcs0.png)
click on Lunch instance.

On the EC2 instance page, enter names for the two instance name, Web01 and Web02, or any name of your choice.

![tag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qp8b0tydj3z4lbsmh1k.png)
Next, To access the website, port 80 have to be allowed. Select the Security group in use for the instances and allow port 80 for inbound rules.

![sg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kufj6448rego63dzqgo.png)
The default port for HTTP traffic on Nginx server is port 80 

![Port 80](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xf2e9byblwkdymwsfm5u.png)
Enter the port and click on save rules.

Now, you should be able to view the website in the browser using the public IP address on both instances.

![Website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0n797k84025170aagz4.png)
We are not done yet, next, we would configure the target group.

**Target Groups**
Target Groups are group of instances with health checks configured. Health checks, when configured enables the ELB to route traffic to only the healthy instances

On the EC2 dashboard, scroll down on the left menu and select target groups.

1. Choose a target type = instances
2. Enter a name
3. Protocol = HTTP
4. port = 80
5. Health Checks 
- Health check protocol = HTTP 
- Health check path is used to check if the website is healthy ( available) or not. For our application, path it is 

> /

leave the default settings and click next.


**_Registered targets_**
Next, We will register the EC2 instances (in the case two, but it can be more ) for this target group. This implies that all healthy instances (or web servers) in this target group would receive equal or close to equal loads/web traffic, ensuring higher availability, scalability, and reliability of your application.
select the two instances and click on _Include as pending below_
![target group setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ib7ijvpq3wqzfhvf3she.png)
Review and click on _Create target group_
![target groups](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyz1amxjvzzjcl8woxc2.png)
Finally, we need to associate the target group a Load balancer. 

- Select the target group
- Click on **_Actions_** and select _**Associate with new load balancer**_

![tg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/daubcamejq83ktk8qq4q.png)
Load balancer name = Enter your desired name
**Scheme** = Internet-facing
**IP address type** = IPv4
**VPC** =  Same VPC used for the EC2 instances
**Mappings** = Select 2 more Availability Zones for the application
Security groups = Create another one, allow port 80 and add the security group for the ec2 instances and select it.

![sg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlhdnmwvnrxc8jo29k4s.png)
**_Listeners and routing_**
**Protocol** = HTTP
**Port** = 80
**Default action** = select the target group you created

![lb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3vng6i5p37wtbinu8qo.png)
Click on create Load balancer.
Wait for some minutes for the Load balancer state to change from _provisioning_ to _Active_.

![lb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75awvqsscmt1c1nzxc2p.png)

**View in Browser**

- Click on the load balancer
- Copy the DNS into the browser. 

![lb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvp9p2r5e821556p3oaw.png)

This serves as the single external endpoint of access for the two web servers. Also ensure that the security group for the EC2 instances only allows port 22 for SSH or AWS session manager for private access to your instances, this ensure that ec2 instances are not accessible to the public directly but only through the Application Load Balancer. This create layers for security for your application.

![web](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o634l3x2p6eijhn6ix1s.png)

As always, I look forward to getting your thoughts on this article. Please feel free to leave a comment!