Skip to content

How do I set up path-based routing on an Application Load Balancer?

3 minute read
2

I want to run several microservices behind my Application Load Balancer. I want to forward requests to specific target groups based on the URL path.

Short description

You can use an Application Load Balancer to create a listener with rules that forward requests to target groups based on the URL. The path pattern rules apply only to the path of the URL and not to the query parameters of the URL.

To establish path-based routing on your Application Load Balancer, first create a target group. Then, configure the listener rules.

Note: You can't use path-based routing for Classic Load Balancer, Network Load Balancer, or Gateway Load Balancer.

Resolution

Prerequisite: Create an Application Load Balancer.

Launch your Amazon EC2 instances

Launch the Amazon Elastic Compute Cloud (Amazon EC2) instances. Register the EC2 instances as targets in a target group within an Amazon Virtual Private Cloud (Amazon VPC). Make sure that the instances' target security groups allow access on the listener port and health check port.

Then, deploy the application on the instances. For an example deployment, see Tutorial: Install a LAMP server on Amazon Linux 2023 (AL2023).

Create a target group

Create a target group, and then register your instances with the target group.

After you specify a target group, the load balancer continually monitors the health of all targets that are in the activated Availability Zone. The load balancer routes requests to the registered targets that are healthy.

Configure listener rules

First, create the HTTP or HTTPS listener that you can define rules in.

To implement path-based routing, configure a listener rule for each path pattern.

Note: Path-based routing rules look for an exact match. If your application requires requests to be routed further on these paths, then include a wildcard in the condition for the path-based routing rule.

Test your path-based routing

To test the path-based routing, copy the DNS name of your Application Load Balancer in a web browser. Add the URL path, such as /svcA or /svcB. Make sure that the path sends you to the correct service.

For example, you configured listener rules for a load balancer with the DNS name alb-demo-1234567890.us-west-2.elb.amazonaws.com. Service A runs on a path in target-group-A and service B runs on a path in target-group-B.

Listener rule 1 

If your request URL path contains the string /svcA, then forward the request to target-group-A.

Listener rule 2: If your request URL path contains the string /svcB, then forward that request to target-group-B.

The http://alb-demo-1234567890.us-west-2.elb.amazonaws.com/svcA URL must return service A and http://alb-demo-1234567890.us-west-2.elb.amazonaws.com/svcB must return service B.

If you use path-based routing, then you can host multiple microservices behind a single Application Load Balancer. To route traffic based on conditions such as the host header, user-agent header, or query parameter values, see Advanced request routing for Application Load Balancers.

Related information

How do I troubleshoot failed health checks for my Application Load Balancer?

Troubleshoot your Application Load Balancers