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):
- Classic Load Balancer (CLB) – Basic load balancer that works on Layer 4 (IP and Port level). Ideal for simple setups.
- 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.
- 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.
- 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
- 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
- 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:
- Check Security Group: Make sure HTTP (port 80) is open.
- Log in to EC2 instance using SSH and check if the web server (Nginx) is running. Use the command
sudo systemctl status nginx
If not, restart it.
Step 2: Creating an AMI (Amazon Machine Image)
Now that our instance is running, let’s create a custom image (AMI).
- Select your EC2 instance → Click Actions → Go to Image & Templates → Click Create Image.
- Name it something like
web01-ami
. - 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.
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:
- 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
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.
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.
- Select Instances as the target type.
- Name it something like
web-TG
. - Set Port 80 (HTTP) since our website runs on this 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).
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.
- Health Check Path:
** Create an Application Load Balancer**
- Go to Load Balancers section → Click Create Load Balancer.
- Select Application Load Balancer (ALB).
- Name it something like
web-ELB
. - Set it to Internet-facing (so users can access it online).
- Choose at least two Availability Zones for high availability but you can select all AZ.
- Create a Security Group for the Load Balancer (web-elb-sg) and allow HTTP (port 80) from anywhere.
- Click on Save rules and your security group will be created.
- 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.
- Copy the DNS in the Load balancer and paster to your browser.
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.
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
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)