How to Create ELB in AWS with EC2 Instances in Different Availability Zones (Step-by-Step Guide)

Flow:
User โ†’ ELB (ALB Listener) โ†’ Target Group โ†’ EC2 Instances (in different AZs)

๐Ÿ“Œ Introduction

In this guide, you will learn how to:

  • Launch 2 EC2 instances in different Availability Zones
  • Create a Target Group
  • Configure an Application Load Balancer (ELB)
  • Distribute traffic for high availability

We will use:

  • Amazon EC2
  • Elastic Load Balancing

๐Ÿ”น Step 1: Launch EC2 Instances (in Different AZs)

๐Ÿ› ๏ธ Create First Instance (AZ-1)

  1. Go to EC2 Dashboard โ†’ Launch Instance
  2. Name: web-server-1
  3. AMI: Amazon Linux
  4. Instance type: t2.micro
  5. Select Availability Zone (e.g., ap-south-1a)
  6. Security Group:
    • Allow HTTP (80)
    • Allow SSH (22)

๐Ÿ‘‰ User Data:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl enable httpd
systemctl start httpd
echo "Server 1 - AZ1 Working" > /var/www/html/index.html

๐Ÿ› ๏ธ Create Second Instance (AZ-2)

Repeat same steps:

  • Name: web-server-2
  • Availability Zone: ap-south-1b

๐Ÿ‘‰ User Data:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl enable httpd
systemctl start httpd
echo "Elb and auto scalling second server working " > /var/www/html/index.html

๐Ÿ”น Step 2: Create Target Group

  1. Go to EC2 โ†’ Target Groups
  2. Click Create Target Group

โš™๏ธ Configuration:

  • Target type: Instances
  • Name: my-target-group
  • Protocol: HTTP
  • Port: 80
  • VPC: Select your VPC

๐Ÿ‘‰ Health Check:

  • Path: /
  • Protocol: HTTP

๐Ÿ‘‰ Register Targets:

  • Select both instances (web-server-1, web-server-2)
  • Click Include as pending
  • Click Create Target Group

๐Ÿ”น Step 3: Create Application Load Balancer (ELB)

  1. Go to EC2 โ†’ Load Balancers
  2. Click Create Load Balancer
  3. Choose: Application Load Balancer

โš™๏ธ Basic Settings:

  • Name: my-alb
  • Scheme: Internet-facing
  • IP type: IPv4

โš™๏ธ Network Mapping:

  • Select at least 2 subnets (different AZs)

โš™๏ธ Security Group:

  • Allow HTTP (Port 80)

โš™๏ธ Listener:

  • Protocol: HTTP
  • Port: 80

โš™๏ธ Routing:

  • Select existing Target Group: my-target-group

๐Ÿ‘‰ Click Create Load Balancer


๐Ÿ”น Step 4: Verify Health Check

  • Go to Target Group โ†’ Targets
  • Status should be: Healthy โœ…

๐Ÿ‘‰ If not:

  • Check Apache is running
  • Check port 80 open

๐Ÿ”น Step 5: Test Load Balancer

  1. Copy Load Balancer DNS
  2. Paste in browser

๐Ÿ‘‰ Output will switch between:

  • Server 1 - AZ1 Working
  • Server 2 - AZ2 Working

๐Ÿ‘‰ This confirms:
โœ… Load balancing working
โœ… Multi-AZ setup working

Total Page Visits: 1691 - Today Page Visits: 59

Leave a Reply

Your email address will not be published. Required fields are marked *