π 03. Creating ELB (Elastic Load Balancer)
In this hands-on exercise, we will:
- Launch two EC2 instances.
- Deploy a simple web app using EC2 User Data.
- Create an Application Load Balancer (ALB) to distribute traffic between the two instances.
- Test load balancing and health checks.
π§ Architecture Diagramβ
[ Client Browser ]
|
βΌ
[ π AWS Application Load Balancer ]
|
ββββββββββββββββ
βΌ βΌ
[ EC2 #1 ] [ EC2 #2 ]
Hello World Hello World
βοΈ A. Launch EC2 Instancesβ
πͺ Step 1: Go to the EC2 Consoleβ
- Navigate to EC2 β Instances β Launch Instances.
πͺ Step 2: Configure Instance Detailsβ
| Setting | Value |
|---|---|
| Name | My First Instance |
| Amazon Machine Image (AMI) | Amazon Linux 2 |
| Instance Type | t2.micro |
| Number of Instances | 1 |
| Key Pair | Proceed without a key pair |
π‘ Tip: Launch each EC2 instance separately, so you can choose a different Availability Zone for each (for example, ap-south-1a and ap-south-1b).
πͺ Step 3: Configure Network Settingsβ
-
Select existing security group β
launch-wizard-1 -
This group should already allow:
- HTTP (port 80)
- SSH (port 22)
πͺ Step 4: Add User Data (Web Server Script)β
Scroll down to Advanced details β User Data and paste the following:
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html
πͺ Step 5: Launch First Instance and Create AMI for Secondβ
-
Click Launch Instances to launch the first instance.
-
Rename the launched instance to:
My First Instance
-
After the first instance is running and configured, create an AMI from it:
- Select the instance β Actions β Image and templates β Create image.
- Give the image a name (for example,
my-first-instance-ami) and create the image.
-
Launch the second instance using the newly created AMI:
-
Navigate to Launch Instances β choose My AMIs β select
my-first-instance-ami. -
Configure Instance Type (
t2.micro) and Network settings (choose the desired Availability Zone). -
Proceed without a key pair if desired.
-
Launch and rename the second instance to:
My Second Instance
-

πͺ Step 6: Verifyβ
-
Wait for both instances to reach Running state.
-
Copy each Public IPv4 address and open in a browser:
-
You should see:
Hello World from ip-...
-

βοΈ B. Create an Application Load Balancer (ALB)β
πͺ Step 1: Navigate to Load Balancersβ
- Go to EC2 β Load Balancers β Create Load Balancer
πͺ Step 2: Select Typeβ
Choose Application Load Balancer (ALB)
| Type | Protocol | Use Case |
|---|---|---|
| Application Load Balancer | HTTP/HTTPS | Web traffic |
| Network Load Balancer | TCP/UDP | Ultra-low latency |
| Gateway Load Balancer | All | Firewalls, inspection tools |
πͺ Step 3: Configure Basic Settingsβ
| Field | Value |
|---|---|
| Name | DemoALB |
| Scheme | Internet-facing |
| Address Type | IPv4 |
πͺ Step 4: Network Mappingβ
- Choose your VPC.
- Select All Availability Zones.
πͺ Step 5: Configure Security Groupβ
- Create a new SG named
demo-sg-load-balancer. - Allow HTTP (port 80) from anywhere (
0.0.0.0/0). - Attach this SG to the ALB (remove the default one).

π― Create Target Groupβ
A Target Group is basically a collection of resources (targets) that your Load Balancer sends traffic to.
These βtargetsβ are usually:
EC2 Instances ,Containers, IP Addresses, Lambda functions
Each target group tells AWS:
βHereβs where to send incoming requests that match certain rules.β
πͺ Step 1: Create Target Groupβ
- Type: Instances
- Name:
demo-tg-alb - Protocol:
HTTP - Port:
80 - Health Check Path:
/
πͺ Step 2: Register Targetsβ
- Select both EC2 instances.
- Click Include as pending below.
- Click Create Target Group.

π Attach Target Group to Load Balancerβ
-
Go back to your ALB creation page.
-
Under Listeners and Routing, choose:
- Listener: HTTP on port 80
- Target Group:
demo-tg-alb
-
Click Create Load Balancer.


π Test Load Balancingβ
πͺ Step 1: Wait for Provisioningβ
- Wait until ALB status = Active.
- Copy the DNS name (e.g.,
demoalb-123456.elb.amazonaws.com).
πͺ Step 2: Access the Load Balancerβ
-
Open the DNS name in a browser.
-
Refresh the page multiple times:
- Youβll see the instance name changes.
- β Confirms load balancing is working.

π Health Check and Fault Toleranceβ
πͺ Step 1: Check Target Healthβ
- Go to Target Groups β demo-tg-alb β Targets.
- Both should show as Healthy.
πͺ Step 2: Simulate Failureβ
-
Stop one instance.
-
Refresh target group β it becomes Unhealthy/Unused.
-
Refresh browser:
- Traffic routes only to the healthy instance.
πͺ Step 3: Recoveryβ
-
Start the stopped instance.
-
It will move from Initial β Healthy status.
-
Refresh browser:
- Requests will again alternate between both instances.
β Summaryβ
| Component | Purpose |
|---|---|
| EC2 Instances | Host simple βHello Worldβ web app |
| Application Load Balancer | Distributes HTTP traffic |
| Target Group | Tracks instance health |
| Security Groups | Control inbound traffic |
| Health Checks | Detect unhealthy instances |
βοΈ Limitationβ
In this step, we are manually launching EC2 instances to understand how web servers are created and managed before automating the process later with Auto Scaling Groups (ASG).
π§ What Weβre Doingβ
Weβre:
- Manually launching two EC2 instances
- Installing Apache (httpd) using User Data
- Placing both behind a Load Balancer
This builds the base understanding of how individual servers are configured and connected.