DEV Community

Israel .O. Ayanda
Israel .O. Ayanda

Posted on • Updated on

Hands-on: AWS Elastic Load Balancer

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

diagram
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
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*
Enter fullscreen mode Exit fullscreen mode

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
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
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
The default port for HTTP traffic on Nginx server is port 80

Port 80
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
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
  6. Health check protocol = HTTP
  7. 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
Review and click on Create target group
target groups
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
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
Listeners and routing
Protocol = HTTP
Port = 80
Default action = select the target group you created

lb
Click on create Load balancer.
Wait for some minutes for the Load balancer state to change from provisioning to Active.

lb

View in Browser

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

lb

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

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

Top comments (0)