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 π
Log in to your AWS Management Console.
Change the region to
ap-south-1
(Mumbai).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 π
Launch 2 EC2 instances.
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 π οΈ
Launch 2 more EC2 instances.
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.
Go to the Elastic Load Balancer section.
Click on Target Groups.
Create Target Group 1:
Name:
tg-home
Add the Home Page instances.
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 π
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
While configuring the ALB, set the default target group to
tg-home
.
Step 6: Add Path-Based Redirect Rules π€οΈ
Go to the Listeners section of your ALB.
Select Rules and click on Edit Rules.
Add a new rule:
Condition: Path is
/test
.Action: Forward traffic to the target group
tg-test
.
Steps to Add Rules π
Click Add Rule.
Set the condition:
Choose Path.
Add
/test
as the value.
Set the action:
Select Forward to and choose
tg-test
.
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 π
Copy the DNS Name of the ALB.
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! π