How to Create Path-Based Redirects for AWS Application Load Balancer 🌐

What are Path-Based Redirects? πŸ€”

Path-based redirects allow you to route specific traffic to different servers or target groups based on the URL path. For example:

  • Traffic to / (home page) can be sent to one group of servers.

  • Traffic to /test (test page) can be sent to another group of servers.

This is particularly useful when managing multiple services or applications under a single load balancer. Let’s dive into the step-by-step guide to set up path-based redirects using AWS Application Load Balancer (ALB).


Steps to Create Path-Based Redirects for ALB πŸš€

Step 1: Log Into Your AWS Root Account πŸ”‘

  1. Log in to your AWS Management Console.

  2. Change the region to ap-south-1 (Mumbai).

  3. Search for EC2 in the search bar and go to the EC2 dashboard.


Step 2: Create Multiple EC2 Instances βš™οΈ

To know about how to create EC2 instances check my blog by clicking here

We’ll create two sets of instances:

  • 2 instances for the Home Page (/)

  • 2 instances for the Test Page (/test)

For Home Page Instances 🏠

  1. Launch 2 EC2 instances.

  2. Use the following User Data script:

#!/bin/bash
sudo yum -y update
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
echo "<h1>My instance is running</h1>" > /var/www/html/index.html
sudo systemctl restart httpd

For Test Page Instances πŸ› οΈ

  1. Launch 2 more EC2 instances.

  2. Use this User Data script to create a /test directory:

#!/bin/bash
sudo yum -y update
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo mkdir /var/www/html/test
echo "<h1>This is for test dir $(hostname)</h1>" > /var/www/html/test/index.html
sudo systemctl restart httpd

πŸ”Ή Pro Tip: Verify that the instances are up and running using their Public IPs.


Step 3: Check Instance Status βœ…

Make sure all 4 instances are running and accessible:

  • Home Page instances: Access http://<instance_public_ip>

  • Test Page instances: Access http://<instance_public_ip>/test


Step 4: Create Target Groups 🎯

We need two target groups:

  • tg-home for Home Page instances.

  • tg-test for Test Page instances.

  1. Go to the Elastic Load Balancer section.

  2. Click on Target Groups.

  3. Create Target Group 1:

    • Name: tg-home

    • Add the Home Page instances.

  4. Create Target Group 2:

    • Name: tg-test

    • Add the Test Page instances.

βœ… Success: Both target groups are ready to handle specific traffic.


Step 5: Create an Application Load Balancer 🌐

  1. Follow the steps to create an Application Load Balancer.

    • If unsure, check my blog on ELB which will help you create an application load balancer click here
  2. While configuring the ALB, set the default target group to tg-home.


Step 6: Add Path-Based Redirect Rules πŸ›€οΈ

  1. Go to the Listeners section of your ALB.

  2. Select Rules and click on Edit Rules.

  3. Add a new rule:

    • Condition: Path is /test.

    • Action: Forward traffic to the target group tg-test.

Steps to Add Rules πŸ“

  1. Click Add Rule.

  2. Set the condition:

    • Choose Path.

    • Add /test as the value.

  3. Set the action:

    • Select Forward to and choose tg-test.

  4. Save the rule.

πŸ”Ή The ALB will now forward /test traffic to the Test Page instances and all other traffic to the Home Page instances.


Step 7: Test the Configuration πŸ”

  1. Copy the DNS Name of the ALB.

  2. Open your browser and test:

    • http://<ALB_DNS> β†’ Should load the Home Page.

    • http://<ALB_DNS>/test β†’ Should load the Test Page.

πŸŽ‰ Success: Path-based redirects are working perfectly!


Wrapping Up 🎯

Path-based redirects in AWS ALB help you optimize traffic routing for specific URL paths. This is an essential feature for modern web applications that need to handle multiple services or microservices under one load balancer.

By following this guide, you can:

  • Set up EC2 instances with specific configurations.

  • Create target groups to organize your instances.

  • Configure path-based rules to route traffic efficiently.

πŸ”Ή Ready to explore more AWS features? Stay tuned for more hands-on guides! πŸš€

Β