DEV Community

Cover image for BUILDING A SCALABLE APPLICATION WITH ELASTIC LOAD BALANCER
Tolulope Olawuni
Tolulope Olawuni

Posted on

BUILDING A SCALABLE APPLICATION WITH ELASTIC LOAD BALANCER

Why Do We Need Multiple Web Servers?

In real life, one web server is not enough to handle high traffic or ensure smooth performance. More so, if that single server crashes, the entire system goes down. But if you have a cluster of web servers, you can:

  • Scale out by adding more servers when demand increases
  • Scale in by reducing servers to cut costs when demand is low
  • Update your servers without downtime

Now, when you have multiple servers, you can’t expect users to manually connect to each one. Instead, we need one central access point, and that’s exactly what a Load Balancer provides.

What Does a Load Balancer Do?

Think of a Load Balancer as a traffic officer. When users send requests, the Load Balancer takes these requests (on common ports like 80 for HTTP or 443 for HTTPS) and distributes them across different servers to balance the load.

Types of Load Balancers in AWS

AWS offers different types of Elastic Load Balancers (ELB):

  1. Classic Load Balancer (CLB) – Basic load balancer that works on Layer 4 (IP and Port level). Ideal for simple setups.
  2. Application Load Balancer (ALB) – Works on Layer 7 (understands URLs). It intelligently routes traffic based on URL paths and is the most commonly used for HTTP/HTTPS traffic.
  3. Network Load Balancer (NLB) – Also operates on Layer 4, but it provides a static IP address and can handle millions of requests per second. It’s more expensive but great for high-performance applications.
  4. Gateway Load Balancer (GLB) – Works on Layer 3 and is mainly used for firewalls and security appliances. This is an advanced concept that requires deep knowledge of AWS VPC networking.

Setting Up a Website on AWS EC2 with Load Balancer

We’re going to set up a simple website using a template from tooplate.com on an AWS EC2 instance. After that, we’ll create a Load Balancer to distribute traffic between multiple instances.

Step 1: Launching an EC2 Instance
Image description

  • Go to the EC2 service on AWS.
  • Click on Launch Instance and give it a name (e.g., web01)
  • Choose an operating system. Let's stick with Ubuntu

If you’re unsure, stick with Amazon Linux since it’s optimized for AWS.

  • Select Instance Type
  • Create a Key Pair:
  • Set Security Group Rules:
    • Create a new Security Group
    • Allow SSH (port 22) from your own IP for security.
    • Allow HTTP (port 80) from your own IP (for website access).
  • Keep Storage Default (8GB is fine)
  • User Data Script: Copy and paste my script below in the code block to automatically install and configure the web server(Nginx).
#!/bin/bash
sudo apt update
sudo apt install unzip wget nginx -y

# Enable and start Nginx
sudo systemctl enable nginx
sudo systemctl start nginx

# Create directory for the website files
mkdir -p /opt/web01
cd /opt/web01

# Download and extract the website template
wget https://www.tooplate.com/zip-templates/2130_waso_strategy.zip
unzip 2130_waso_strategy.zip

# Move files to Nginx's web root
sudo mv 2130_waso_strategy/* /var/www/html/

# Restart Nginx to apply changes
sudo systemctl restart nginx

Enter fullscreen mode Exit fullscreen mode
  • Click Launch Instance and wait for a few minutes.
  • Check the Website: Copy the public IP address of the instance and paste it in your browser. If everything works, you should see your website

Troubleshooting

If the website doesn’t load:

  1. Check Security Group: Make sure HTTP (port 80) is open.
  2. Log in to EC2 instance using SSH and check if the web server (Nginx) is running. Use the command
sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

If not, restart it.


Step 2: Creating an AMI (Amazon Machine Image)

Image description

Now that our instance is running, let’s create a custom image (AMI).

  1. Select your EC2 instance → Click Actions → Go to Image & Templates → Click Create Image.
  2. Name it something like web01-ami.
  3. Click Create Image and wait a few minutes.

With this AMI, you can launch multiple instances with the same configuration without setting up everything from scratch.

Image description
You can click the AMI section to view the created AMI.
NOTE: While both snapshots and AMI are use for backup on AWS, Snapshots are the backup of an EBS volume, while AMIs are backup for your EC2 instance plus the metadata.

Step 3: Launch Template for Quick Instance Deployment

Instead of repeating the setup process every time of creating AMIs, we can create a Launch Template:

Image description

  • Go to Launch Templates → Click Create Launch Template.
  • Name it web-template-v1.
  • Select the AMI we just created.
  • Choose instance type (t2.micro), security group, and key pair.
  • Save the template Image description

Now, whenever you need a new instance, just launch it from this template in seconds instead of setting up everything manually! I will use this Launch template to create another instance so we can multiple servers for our load balancer.

Image description
After clicking on the the launch instance, you might decide to not modify the options and launch instance from the template

Step 4: Setting Up a Load Balancer

Since we now have multiple instances, users need a single endpoint to access the website. That endpoint will route users requests to any of the instances created. This is where the Load Balancer comes in.

** Create a Target Group**

We will go to the target group section to creat target group, which is just a group of Instances
A Target Group groups multiple instances together:

  • Go to Target Groups → Click Create Target Group.

Create Load balancer

  • Select Instances as the target type. Instance Load balancer
  • Name it something like web-TG.
  • Set Port 80 (HTTP) since our website runs on this port. SET PORT Configure Health Checks:
    • Health Check Path: / (Root of the website).
    • Healthy threshold: 2 (checks twice before declaring it healthy).
    • Unhealthy threshold: 2 (declares it unhealthy after 2 failures). Image description Click on the next button, where it will show us our 2 instances; the one we created from the scratch(web01) and the other we created from the Launch template(web02). Select the 2 instances and click on include as pending below to add it to our target group. Add your EC2 instances and click Create Target Group.

Create Target group

** Create an Application Load Balancer**

Image description

  • Go to Load Balancers section → Click Create Load Balancer.
  • Select Application Load Balancer (ALB).

Select Application Load balancer

  • Name it something like web-ELB.
  • Set it to Internet-facing (so users can access it online). Image description
  • Choose at least two Availability Zones for high availability but you can select all AZ. Image description
  • Create a Security Group for the Load Balancer (web-elb-sg) and allow HTTP (port 80) from anywhere. Image description
  • Click on Save rules and your security group will be created.

Image description

  • Now go back to the Load balancer tab, refresh and this newly created security group will be available for selection as seen above. The listener(HTTP) is the front-end while the Target group is the backend. The listener will be routing request to the Target group. The target group(web-TG) we created can be selected form the drop down. we scroll down and create our Load balancer.
  • Testing the Load Balancer** Once the Load Balancer is active:
  • Copy the Load Balancer DNS name.
  • Paste it into your browser.
  • Your website should now be accessible through the Load Balancer, which distributes traffic between multiple instances.

Elastic Load Balancer Created

  • Copy the DNS in the Load balancer and paster to your browser. Image description The website won't open, because there's something we failed to do.

Troubleshooting Load Balancer Issues

Every instance has it's own security group, we checked the inbound rule of the 2 instances it allows port 80 from my IP and not from the Load balancer. So we have to add the rule to the Instances, so it can allow connection from the Load balance which is the security group we created for the load balancer.

Image description
You remember the security group we created for the ELB...ELB-SG! that' what we added to the instance inbound rule.

Final Thoughts

We have successfully:

  • Launched an EC2 instance with a website.
  • Created a custom AMI for easy scaling.
  • Created a Launch Template for quick deployment.
  • Set up a Load Balancer to distribute traffic between multiple instances. Our website from tooplate Image description This setup ensures high availability and makes it easy to add more instances when traffic increases.

Keep practicing, and you’ll soon master AWS deployments!
I hope this has been a good read for us, if you have any suggestion to improve this article, please feel free to reach out.
Thanks

Top comments (0)