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

Published: (December 9, 2025 at 04:41 AM EST)
3 min read
Source: Dev.to

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

  1. Open VPC Dashboard → Your VPCs → Create VPC.
  2. Settings:
    • Name: ecommerce-vpc
    • IPv4 CIDR: 12.0.0.0/16
  3. Click Create.

Create Two Public Subnets

SubnetNameAvailability ZoneCIDR
1public-subnet-1ae.g., us-east-2a12.0.1.0/24
2public-subnet-2bus-east-2b12.0.3.0/24

Create each subnet under the ecommerce-vpc.

Create and Attach an Internet Gateway

  1. Internet Gateways → Create.
  2. Name: ecommerce-igw.
  3. After creation, attach it to ecommerce-vpc.

Configure Route Table for Public Access

  1. Go to Route Tables, select the main route table for your VPC.
  2. Edit Routes → Add:
    • Destination: 0.0.0.0/0
    • Target: ecommerce-igw
  3. Associate this route table with both public subnets (12.0.1.0/24 and 12.0.3.0/24).

Launch Two Ubuntu EC2 Instances

InstanceSubnetPublic IPSecurity Group
1public-subnet-1a (12.0.1.0/24)Auto‑assign āœ…web-sg
2public-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/0 for testing)

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)

  1. EC2 → Load Balancers → Create Load Balancer → Application Load Balancer.
  2. Settings:
    • Name: ecommerce-alb
    • Scheme: Internet‑facing
    • IP address type: IPv4
    • Listeners: HTTP (port 80)
    • Availability Zones: select ecommerce-vpc and both public subnets (12.0.1.0/24, 12.0.3.0/24)
    • Security Group: alb-sg (allow HTTP from 0.0.0.0/0)

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

  1. Wait 2–5 minutes for the ALB to become active.
  2. Copy the DNS name from the console (e.g., ecommerce-alb-xxxxxx.us-east-2.elb.amazonaws.com).
  3. Open http:// in a browser.
  4. 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

  1. Prepare your local project folder (e.g., Electro).

  2. Set secure permissions on your SSH key:

    chmod 400 test-ALB-demo.pem
  3. 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/
  4. 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
  5. 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 website
    • http://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.

Back to Blog

Related posts

Read more Ā»