DEV Community

Haripriya Veluchamy
Haripriya Veluchamy

Posted on

AWS Load Balancer Setup: ALB and NLB Implementation Guide 🚀

Image description

Introduction 👋

In this guide, I'll share my hands-on experience setting up both Application Load Balancer (ALB) and Network Load Balancer (NLB) in AWS. This implementation includes path-based routing and TCP traffic handling.

Prerequisites 📝

  • AWS Account with appropriate permissions
  • Basic understanding of AWS networking concepts
  • Understanding of load balancing principles

Infrastructure Components 🏗️

Our setup includes:

  • VPC with public and private subnets
  • Internet Gateway
  • Route Tables
  • Security Groups
  • EC2 instances
  • Application Load Balancer (ALB)
  • Network Load Balancer (NLB)

Step-by-Step Implementation Guide 🛠️

1. VPC Setup

# Create VPC
aws ec2 create-vpc --cidr-block 10.0.0.0/16

# Create Subnets
aws ec2 create-subnet --vpc-id <vpc-id> --cidr-block 10.0.1.0/24 # Public Subnet
aws ec2 create-subnet --vpc-id <vpc-id> --cidr-block 10.0.2.0/24 # Private Subnet 1
aws ec2 create-subnet --vpc-id <vpc-id> --cidr-block 10.0.3.0/24 # Private Subnet 2

# Create and attach Internet Gateway
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --vpc-id <vpc-id> --internet-gateway-id <igw-id>
Enter fullscreen mode Exit fullscreen mode

2. Application Load Balancer (ALB) Configuration 🌐

  1. Create Target Groups:
# Create target group for /foo path
aws elbv2 create-target-group \
    --name foo-target-group \
    --protocol HTTP \
    --port 80 \
    --vpc-id <vpc-id>

# Create target group for /bar path
aws elbv2 create-target-group \
    --name bar-target-group \
    --protocol HTTP \
    --port 80 \
    --vpc-id <vpc-id>
Enter fullscreen mode Exit fullscreen mode
  1. Create ALB:
aws elbv2 create-load-balancer \
    --name my-application-lb \
    --subnets <subnet-1> <subnet-2> \
    --security-groups <security-group-id> \
    --type application
Enter fullscreen mode Exit fullscreen mode
  1. Configure Listeners with Path-Based Routing:
aws elbv2 create-listener \
    --load-balancer-arn <alb-arn> \
    --protocol HTTP \
    --port 80 \
    --default-actions \
    Type=forward,TargetGroupArn=<default-target-group-arn>
Enter fullscreen mode Exit fullscreen mode

3. Network Load Balancer (NLB) Setup ⚡

  1. Create TCP Target Group:
aws elbv2 create-target-group \
    --name tcp-target-group \
    --protocol TCP \
    --port 80 \
    --vpc-id <vpc-id>
Enter fullscreen mode Exit fullscreen mode
  1. Create NLB:
aws elbv2 create-load-balancer \
    --name my-network-lb \
    --type network \
    --subnets <subnet-id>
Enter fullscreen mode Exit fullscreen mode

4. EC2 Instance Configuration 💻

  1. Launch EC2 instances in private subnets:
aws ec2 run-instances \
    --image-id ami-12345678 \
    --instance-type t2.micro \
    --subnet-id <private-subnet-id> \
    --user-data file://user-data.sh
Enter fullscreen mode Exit fullscreen mode
  1. User Data Script for /foo instance:
#!/bin/bash
apt-get update -y
apt-get install -y apache2
systemctl start apache2
systemctl enable apache2
mkdir -p /var/www/html/foo
echo "<h1>Welcome to foo</h1>" > /var/www/html/foo/index.html
Enter fullscreen mode Exit fullscreen mode
  1. User Data Script for /bar instance:
#!/bin/bash
apt-get update -y
apt-get install -y apache2
systemctl start apache2
systemctl enable apache2
mkdir -p /var/www/html/bar
echo "<h1>Welcome to bar</h1>" > /var/www/html/bar/index.html
Enter fullscreen mode Exit fullscreen mode

Image description

  1. ALB Security Group:
aws ec2 create-security-group \
    --group-name alb-sg \
    --description "Security group for ALB"

aws ec2 authorize-security-group-ingress \
    --group-id <security-group-id> \
    --protocol tcp \
    --port 80 \
    --cidr 0.0.0.0/0
Enter fullscreen mode Exit fullscreen mode

Testing and Verification ✅

  1. Test ALB Path-Based Routing:

    • Access http://<alb-dns>/foo
    • Access http://<alb-dns>/bar
  2. Test NLB TCP Connection:

    • Use netcat or telnet to test TCP connectivity
    • Verify static IP assignment

Monitoring and Maintenance 📊

  1. CloudWatch Metrics to Monitor:

    • RequestCount
    • TargetResponseTime
    • HealthyHostCount
    • UnHealthyHostCount
  2. Set up CloudWatch Alarms:

aws cloudwatch put-metric-alarm \
    --alarm-name ALB-HighLatency \
    --metric-name TargetResponseTime \
    --namespace AWS/ApplicationELB \
    --statistic Average \
    --period 300 \
    --threshold 5 \
    --comparison-operator GreaterThanThreshold
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Tips 🔍

Common issues and solutions:

  1. Health Check Failures:

    • Verify security group rules
    • Check target group settings
    • Validate instance health
  2. Routing Issues:

    • Confirm listener rules
    • Verify path configurations
    • Check target group attachments

Conclusion 🎉

This setup provides a robust load balancing solution with:

  • Path-based routing using ALB
  • TCP traffic handling with NLB
  • High availability across multiple subnets
  • Proper security configurations

Top comments (1)

Collapse
 
juniourrau profile image
Ravin Rau

Clear step-by-step guide. Thanks for sharing this.