š Load-Balanced Electro E-Commerce Website on AWS (Using AWS Load Balancer)
Source: Dev.to
Overview
This project demonstrates how to deploy a highly available eācommerce website on AWS using:
- Two EC2 instances running Apache
- An Application Load Balancer (ALB) that distributes HTTP traffic
- A custom VPC with public subnets and an Internet Gateway
Perfect for beginners learning AWS infrastructure, load balancing, and high availability.
Architecture
- VPC CIDR:
12.0.0.0/16 - Public SubnetāÆ1:
12.0.1.0/24ā hosts EC2 InstanceāÆ1 - Public SubnetāÆ2:
12.0.3.0/24ā hosts EC2 InstanceāÆ2 - Load Balancer: Application Load Balancer (HTTP on portāÆ80)
- Target Group: Routes traffic to both healthy EC2 instances
- Internet Gateway: Provides public internet access
Create a Custom VPC
- Open VPC Dashboard ā Your VPCs ā Create VPC.
- Settings:
- Name:
ecommerce-vpc - IPv4 CIDR:
12.0.0.0/16
- Name:
- Click Create.
Create Two Public Subnets
| Subnet | Name | Availability Zone | CIDR |
|---|---|---|---|
| 1 | public-subnet-1a | e.g., us-east-2a | 12.0.1.0/24 |
| 2 | public-subnet-2b | us-east-2b | 12.0.3.0/24 |
Create each subnet under the ecommerce-vpc.
Create and Attach an Internet Gateway
- Internet Gateways ā Create.
- Name:
ecommerce-igw. - After creation, attach it to
ecommerce-vpc.
Configure Route Table for Public Access
- Go to Route Tables, select the main route table for your VPC.
- Edit Routes ā Add:
- Destination:
0.0.0.0/0 - Target:
ecommerce-igw
- Destination:
- Associate this route table with both public subnets (
12.0.1.0/24and12.0.3.0/24).
Launch Two Ubuntu EC2 Instances
| Instance | Subnet | Public IP | Security Group |
|---|---|---|---|
| 1 | public-subnet-1a (12.0.1.0/24) | Autoāassign ā | web-sg |
| 2 | public-subnet-2b (12.0.3.0/24) | Autoāassign ā | web-sg |
- AMI: UbuntuāÆ22.04 LTS (x86_64)
- Instance type:
t2.micro(Free Tier eligible) - Security Group (
web-sg) inbound rules:- HTTP (portāÆ80) ā
0.0.0.0/0 - SSH (portāÆ22) ā your IP (or
0.0.0.0/0for testing)
- HTTP (portāÆ80) ā
User Data Script (install Apache)
#!/bin/bash
# Update system packages
apt-get update -y
# Install Apache2
apt-get install -y apache2
# Ensure Apache starts on boot
systemctl enable apache2
systemctl start apache2
# Ensure /var/www/html exists and has correct ownership
mkdir -p /var/www/html
chmod -R 755 /var/www/html
# Optional: Create a simple test page
cat /var/www/html/index.html
# Apache2 is running on Server1
# Instance provisioned automatically on Server1.
EOF
(Use the same script for both instances; you can customize the message for ServerāÆ2.)
Create an Application Load Balancer (ALB)
- EC2 ā Load Balancers ā Create Load Balancer ā Application Load Balancer.
- Settings:
- Name:
ecommerce-alb - Scheme: Internetāfacing
- IP address type: IPv4
- Listeners: HTTP (portāÆ80)
- Availability Zones: select
ecommerce-vpcand both public subnets (12.0.1.0/24,12.0.3.0/24) - Security Group:
alb-sg(allow HTTP from0.0.0.0/0)
- Name:
Target Group
- Create new:
ecommerce-tg - Protocol: HTTP, Port: 80
- Health check path:
/ - Register the two Ubuntu EC2 instances and set them to Include as pending.
Review and create the ALB.
Test the Load Balancer
- Wait 2ā5āÆminutes for the ALB to become active.
- Copy the DNS name from the console (e.g.,
ecommerce-alb-xxxxxx.us-east-2.elb.amazonaws.com). - Open
http://in a browser. - Refresh several times; you should see alternating messages such as:
- āServerāÆ1 (Subnet:āÆ12.0.1.0/24)ā
- āServerāÆ2 (Subnet:āÆ12.0.3.0/24)ā
Both instances should show Healthy in the target group.
Deploy Your Website Files
-
Prepare your local project folder (e.g.,
Electro). -
Set secure permissions on your SSH key:
chmod 400 test-ALB-demo.pem -
Copy files to ServerāÆ1 (replace the public IP with your instanceās IP):
scp -i test-ALB-demo.pem -r Electro/* ubuntu@3.139.70.220:/tmp/ -
Move files into Apacheās web folder on ServerāÆ1:
ssh -i test-ALB-demo.pem ubuntu@3.139.70.220 sudo rm -rf /var/www/html/* sudo mv /tmp/* /var/www/html/ exit -
Repeat for ServerāÆ2 (replace the IP accordingly):
scp -i test-ALB-demo.pem -r Electro/* ubuntu@18.217.149.70:/tmp/ ssh -i test-ALB-demo.pem ubuntu@18.217.149.70 sudo rm -rf /var/www/html/* sudo mv /tmp/* /var/www/html/ exit
Verify
-
Direct access:
http://3.139.70.220ā your websitehttp://18.217.149.70ā same website
-
Through the ALB: open the ALB DNS name; refreshing should serve the site from both servers.
Cleanāup (optional)
When youāre finished, remember to terminate the EC2 instances, delete the ALB, target group, security groups, subnets, and the VPC to avoid ongoing charges.