Complete a 3 Question Survey and Earn a re:Post Badge
Help improve AWS Support Official channel in re:Post and share your experience - complete a quick three-question survey to earn a re:Post badge!
How do I set up path-based routing on an Application Load Balancer?
I plan 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. For more information about path patterns, see Path conditions.
Note: This feature isn't supported for Classic Load Balancer, Network Load Balancer, or Gateway Load Balancer.
To establish path-based routing on your Application Load Balancer, complete these steps:
- Create a target group.
- Configure listener rules.
Prerequisites:
- Launch the Amazon Elastic Compute Cloud (Amazon EC2) instances in an Amazon Virtual Private Cloud (Amazon VPC). For more information, see Get started with Amazon EC2.
- Check that the target security groups of these Amazon EC2 instances allow access on the listener port and health check port.
- Deploy the application on the Amazon EC2 instances that you intend to register with target groups. For an example, see Tutorial: Install a LAMP server on AL2023.
- Create an Application Load Balancer.
Resolution
Create a target group
After you create the instances, register them with a target group. Based on the configured listener rules, the Application Load Balancer sends requests to registered targets based on the port and protocol that you specified. However, you can override the port information when you individually register targets. For more information, see Create a target group for your Application Load Balancer.
For example, suppose that you have two services, service A and service B. The applications that run these services use port 80. Service A runs an application on the path /svcA, and service B runs an application on path /svcB.
- You create two target groups with Protocol as HTTP and Port as 80, each with an application deployed.
- You register the Amazon EC2 instance that runs service A with target-group-A. For this target group, you can set HealthCheckProtocol as HTTP and set HealthCheckPath as /svcA.
- You register the Amazon EC2 instance that runs service B with target-group-B. For this target group, you can set HealthCheckProtocol as HTTP and set HealthCheckPath as /svcB.
You can add or remove targets to or from your target groups at any time. For more information, see Register targets with your Application Load Balancer 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. For more information, see Health checks for Application Load Balancer target groups.
Configure listener rules
When you create a listener for an Application Load Balancer, you can define one or more rules in addition to the default rule. A rule consists of a priority, an action, and one or more conditions. You can't define conditions for the default rule. If none of the conditions for the defined rules are met, then the action for the default rule is performed.
- To learn more about rule priority, see Update rule priority.
- To learn more about rule actions, see Rule action types.
- To learn more about rule conditions, see Rule condition types.
To implement path-based routing on an Application Load Balancer, you must configure listener rules. Configure one rule for each path pattern based on where you want to route your requests.
Listener rules example
The following example shows listener rules for services A and B:
Listener rule 1: If your request URL path contains the string /svcA, then forward the request to target-group-A. You do this because target-group-A includes service A that runs an application on the given path.
Listener rule 2: If your request URL path contains the string /svcB, then forward that request to target-group-B. You do this because target-group-B includes service B that runs an application on the given path.
- To create a new HTTP listener, see Create an HTTP listener for your Application Load Balancer.
- To create a new HTTPS listener, see Create an HTTPS listener for your Application Load Balancer.
To update listener rules with conditions and actions, complete the following steps:
- Open the Amazon EC2 console.
- In the navigation pane, under Load Balancing, choose Load Balancers.
- Choose the load balancer, and then choose Listeners and rules.
- To update the listener, choose the Listeners and rules tab, and then select the text in the Protocol:Port column to open the detail page for the listener.
- On the Rules tab, in the Listener rules section, select the rule you want to edit.
- Choose Actions, then choose Edit rule.
- Choose the Add rules.
- Modify the name and tags as needed. To add additional tags, choose Add additional tags.
- Choose Next.
- To add a path-based rule for /svcA, choose Add condition, choose Path, and then enter the path pattern /svcA. Choose Confirm.
- Choose Next.
- To add a forward action, choose Routing actions as Forward to target groups, and then, on the menu, choose the target group target-group-A.
- Choose Next.
- Choose the appropriate priority.
- Review your details and updated configurations for your rule. When you're satisfied with your selections, choose Save changes.
Repeat the preceding steps for the path /svcB with the following changes:
- For step 10, enter the path pattern /svcB.
- For step 12, choose the target group target-group-B f.
For more information, see Listener rules for your Application Load Balancer.
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. Use path patterns similar to /svcA* or /svcB* to account for any documents on these paths when routing requests.
Test path-based routing
To test path-based routing, copy the DNS name of your Application Load Balancer in a web browser, and then add the URL path /svcA or /svcB.
For example, suppose that your Application Load Balancer has alb-demo-1234567890.us-west-2.elb.amazonaws.com as the DNS name.
- http://alb-demo-1234567890.us-west-2.elb.amazonaws.com/svcA must return service A.
- http://alb-demo-1234567890.us-west-2.elb.amazonaws.com/svcB must return service B.
When the Application Load Balancer listener receives the request, the listener forwards that request to the appropriate target group based on the path condition.
If you use path-based routing, you can host multiple microservices behind a single Application Load Balancer. You can route traffic to each service based on listener rules and target groups. You can also route traffic based on conditions such as the host header, user-agent header, or query parameter values. For more information, see Advanced Request Routing for AWS Application Load Balancers.
Related information
How do I troubleshoot failed health checks for Application Load Balancers?
Related videos


Relevant content
- Accepted Answerasked 2 years agolg...
- Accepted Answerasked a year agolg...
- asked 3 years agolg...
- Accepted Answerasked 8 months agolg...
- AWS OFFICIALUpdated a year ago