Skip to main content

🌐 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​

SettingValue
NameMy First Instance
Amazon Machine Image (AMI)Amazon Linux 2
Instance Typet2.micro
Number of Instances1
Key PairProceed 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
IAM Roles Example

πŸͺœ 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-...
IAM Roles Example

βš–οΈ 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)

TypeProtocolUse Case
Application Load BalancerHTTP/HTTPSWeb traffic
Network Load BalancerTCP/UDPUltra-low latency
Gateway Load BalancerAllFirewalls, inspection tools

πŸͺœ Step 3: Configure Basic Settings​

FieldValue
NameDemoALB
SchemeInternet-facing
Address TypeIPv4

πŸͺœ 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).
IAM Roles Example

🎯 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.
IAM Roles Example

πŸš€ 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.

IAM Roles Example
IAM Roles Example

πŸ” 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.
IAM Roles Example

πŸ’š 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​

ComponentPurpose
EC2 InstancesHost simple β€œHello World” web app
Application Load BalancerDistributes HTTP traffic
Target GroupTracks instance health
Security GroupsControl inbound traffic
Health ChecksDetect 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.


⚑ Why Manual Launch Isn’t Ideal​


LimitationExplanation
❌ Manual scalingYou must add/remove instances by hand
❌ No fault recoveryIf one instance fails, it won’t auto-restart
❌ Inefficient for traffic spikesLoad changes aren’t automatically handled

πŸš€ Real-World Approach​

In production, we use Auto Scaling Groups (ASG) with a Load Balancer to:

  • Automatically launch instances across multiple AZs
  • Replace unhealthy instances
  • Scale up/down with traffic demand

🧩 Big Picture​

We’re doing it manually now to understand the building blocks.
Later, ASG + Load Balancer will automate everything we just did.