DEV Community

Cover image for πŸ›’ Optimizing Electro E-Commerce with AWS Load Balancer Solutions
Olaolu' Afolami
Olaolu' Afolami

Posted on

πŸ›’ Optimizing Electro E-Commerce with AWS Load Balancer Solutions

πŸ›’ Load-Balanced Electro E-Commerce Website on AWS (Using AWS Load Balancer)

This project demonstrates how to deploy a highly available e-commerce website using Amazon Web Services (AWS). It uses two EC2 instances, an Application Load Balancer (ALB), and a custom VPC with public subnets β€” configured exactly as shown in the architecture diagram.

βœ… Perfect for beginners learning AWS infrastructure, load balancing, and high availability!


🎯 Project Goal

  • Deploy a Electro e-commerce website on two EC2 instances.
  • Distribute incoming web traffic across both instances using an Application Load Balancer.
  • Build a secure and scalable network architecture using VPC, subnets, and an Internet Gateway.

πŸ—οΈ Architecture Overview

  • VPC: 12.0.0.0/16
  • Public Subnets:
    • 12.0.1.0/24 β†’ Hosts EC2 Instance 1
    • 12.0.3.0/24 β†’ Hosts EC2 Instance 2
  • EC2 Instances: 2 web servers (running Apache)
  • Load Balancer: Application Load Balancer (HTTP on port 80)
  • Target Group: Routes traffic to both healthy EC2 instances
  • Internet Gateway: Enables public internet access

image


πŸš€ Step-by-Step Implementation

1. *Create a Custom VPC *

  • Go to VPC Dashboard β†’ Your VPCs β†’ Create VPC
    • Name: ecommerce-vpc
    • IPv4 CIDR: 12.0.0.0/16
  • Click Create

image


2. *Create Two Public Subnets *

  • Subnet 1:
    • Name: public-subnet-1a
    • VPC: ecommerce-vpc
    • Availability Zone: e.g., us-east-2a
    • CIDR: 12.0.1.0/24 ← Matches diagram
  • Subnet 2:
    • Name: public-subnet-2b
    • AZ: us-east-2b
    • CIDR: 12.0.3.0/24

image
image


3. Create and Attach an Internet Gateway

  • Go to Internet Gateways β†’ Create
    • Name: ecommerce-igw
  • After creation, attach it to ecommerce-vpc

image
image


4. 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
  • Associate this route table with both public subnets (12.0.1.0/24 and 12.0.3.0/24)

image
image
image
image


5. Launch Two Ubuntu EC2 Instances

Launch two EC2 instances using the Ubuntu 22.04 LTS AMI:

  • AMI: Ubuntu 22.04 LTS (x86_64) β€” search in AMI catalog
  • Instance Type: t2.micro (Free Tier eligible)
  • Network: ecommerce-vpc
  • Subnet:
    • Instance 1 β†’ public-subnet-1a (12.0.1.0/24)
    • Instance 2 β†’ public-subnet-2b (12.0.3.0/24)
  • Auto-assign public IP: βœ… Enable
  • Security Group: Create new web-sg with:
    • Inbound Rules:
    • HTTP (Port 80) β†’ Source: 0.0.0.0/0
    • SSH (Port 22) β†’ Your IP (or 0.0.0.0/0 for testing)
  • Paste the script below in the data field of the EC2 instance under "Additional settings" to install Apache2 and update the server:
  #!/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
echo "<h1>Apache2 is running on Server1</h1><p>Instance provisioned automatically on Server1.</p>" > /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

image
image
image
image

image


Repeat step 5 to create the second EC2 instance

- In the echo section of the script, change the h1 tag to Apache2 is running on Server2 to distinguish the servers.

6. Create an Application Load Balancer (ALB)

  • Go to EC2 β†’ Load Balancers β†’ Create Load Balancer
  • Choose Application Load Balancer
  • Name: ecommerce-alb
  • Scheme: Internet-facing
  • IP address type: IPv4
  • Listeners: HTTP (Port 80)
  • Availability Zones: Select your VPC and both public subnets (12.0.1.0/24 and 12.0.3.0/24)
  • Security Group: Create or select alb-sg allowing HTTP from 0.0.0.0/0
  • Target Group:
  • Create new: ecommerce-tg
  • Protocol: HTTP, Port: 80, Health check path: /
  • Register both Ubuntu EC2 instances by selecting them and click Include as pending below.
  • Review and Create image image image image image image image

7. Test the Load Balancer

  • Wait 2–5 minutes for the ALB to become active
  • Copy the DNS name of your ALB from the AWS console
  • Open in browser: http://<your-alb-dns-name>
  • Refresh multiple times β€” you should see alternating messages:
  • β€œServer 1 (Subnet: 12.0.1.0/24)”
  • β€œServer 2 (Subnet: 12.0.3.0/24)”
  • Confirm both instances show Healthy in Target Groups image

image


8. Deploy Your Website Files on Ubuntu

Upload your website files (e-commerce web files)
To upload you website files:

πŸ”Ή Step 1: Open Git Bash in Your Website Folder
βœ… Open your terminal and navigate to your project folder.

C:\Users\laolu\Documents\Realprojects\AWS projects\ALB-ecommerce\Electro
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 2: Set Secure Permissions on Your Key

chmod 400 test-ALB-demo.pem
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 3: Copy Files to Server 1 (3.139.70.220)
βœ… Run this command (all on one line):

scp -i test-ALB-demo.pem -r Electro/* ubuntu@3.139.70.220:/tmp/
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 4: Move Files into Web Folder on Server 1
βœ… Now log in to Server 1:

ssh -i test-ALB-demo.pem ubuntu@3.139.70.220
Enter fullscreen mode Exit fullscreen mode

βœ… Once logged in, run these 3 commands (copy/paste one at a time):

# 1. Delete default Apache files
sudo rm -rf /var/www/html/*
Enter fullscreen mode Exit fullscreen mode
# 2. Move your files from /tmp to the web folder
sudo mv /tmp/* /var/www/html/
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 5: Copy Files to Server 2 (18.217.149.70)
βœ… Run this command (all on one line):

scp -i test-ALB-demo.pem -r Electro/* ubuntu@18.217.149.70:/tmp/
Enter fullscreen mode Exit fullscreen mode

βœ… Wait for the files to finish copying.

πŸ”Ή Step 6: Move Files into Web Folder on Server 2
Enter fullscreen mode Exit fullscreen mode

βœ… Log in to Server 2:

ssh -i test-ALB-demo.pem ubuntu@18.217.149.70
Enter fullscreen mode Exit fullscreen mode

βœ… Run the same 3 commands:

sudo rm -rf /var/www/html/*
sudo mv /tmp/* /var/www/html/
exit

Enter fullscreen mode Exit fullscreen mode

βœ… Done! πŸŽ‰
Your entire E-commerce website is now live on both servers.

πŸ” Test It Out

  • Open your browser and go to:
  • ➀ http://3.139.70.220 β†’ should show your real website
  • ➀ http://18.217.149.70 β†’ same website

  • From your Load Balancer, visit its DNS name and refresh β€” you should see the same site served from both servers.

  • Load Balancer DNS: http://ecommerce-alb-178280318.us-east-2.elb.amazonaws.com/

image
image
image
image
image

Top comments (0)