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

6 minute read
2

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

An Application Load Balancer allows you 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 on path patterns, see Path conditions.

Note: This feature isn't available for other load balancer types, including Classic Load Balancer, Network Load Balancer, or Gateway Load Balancer.

To establish path-based routing on your Application Load Balancer, complete these steps:

  1. Create a target group.
  2. Configure listener rules.

Prerequisites

Before you create the target groups, complete the following prerequisites:

Resolution

Create a target group

After you create the instances, register them with a target group. Based on the configured listener rules, the load balancer sends requests to registered targets using 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 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 EC2 instance that's running service A with target-group-A. For this target group, you can set HealthCheckProtocol as HTTP and set HealthCheckPath as /svcA.
  • You register the EC2 instance that's running 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 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 your 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 implement path-based routing on an Application Load Balancer, you must configure listener rules. You must 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 update listener rules with conditions and actions, complete the following steps:

  1. Open the Amazon EC2 console.
  2. In the navigation pane, under Load Balancing, choose Load Balancers.
  3. Select the load balancer, and then choose Listeners.
  4. To update the listener, choose View/edit rules.
  5. Choose the Add rules icon (the plus sign) in the menu bar. This adds Insert Rule icons at the locations where you can insert a rule in the priority order.
  6. Choose one of the Insert Rule icons added in the previous step.
  7. To add a path-based rule for /svcA, choose Add condition, Path, and then enter the path pattern /svcA. To save the condition, choose the checkmark icon.
  8. To add a forward action, choose Add action, Forward to, and then choose the target group target-group-A.
  9. Choose Save.

Repeat the preceding steps for the path /svcB with the following changes:

  • For step 6, enter the path pattern /svcB.
  • For step 7, choose the target group target-group-B.

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.

  • must return service A.
  • 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, then your Application Load Balancer allows you to host multiple microservices behind a single load balancer using listener rules and target groups. You can set up complex rules to route client requests to your applications. In addition to path-based rules, you can route requests to applications based on host header, user-agent header, and 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?

Troubleshoot your Application Load Balancers

AWS OFFICIAL
AWS OFFICIALUpdated 6 months ago