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

6 minute read
1

I'm running 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 forwards requests to target groups based on the URL. This feature isn't available for other load balancer types, such as Classic Load Balancer, Network Load Balancer, and Gateway Load Balancer. The path pattern rules are applied only to the path of the URL and not to URL's query parameters. For more information on path patterns, see Path conditions.

To establish path-based routing on your Application Load Balancer, do the following:

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

Before creating the target groups, be sure that the following prerequisites are met:

Resolution

Suppose that you have two services, service A and service B, with applications running on these services on port 80. For example, service A runs an application on the path /svcA, and service B runs an application on path /svcB.

Create a target group

After your instances are created, register these instances with a target group. Based on the listener rules configured on the load balancer, requests are forwarded to registered targets using the port and protocol that you specified for the target group. 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 create two target groups with Protocol as HTTP and Port as 80, each with an application deployed. For example, 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 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 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 in a rule for a listener, the load balancer continually monitors the health of all targets registered with the target group that are in the Availability Zone enabled for the load balancer. 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 conditions for none of the non-default rules are met, then the action for the default rule is performed.

To learn more about rule priority, see Reorder rules.

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. You must configure one rule for each path pattern based on which you want to route your requests.

For example:

Listener rule 1: If your request URL path contains the string /svcA, then forward the request to target-group-A. This is 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. This is 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.

To create a new HTTPS listener, see Create an HTTPS listener.

To update listener rules with conditions and actions, do the following:

  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. In this example, path-based routing uses the path definitions /svcA and /svcB. If your application requires requests to be routed further down these paths, for example, /svcA/doc or /svcB/doc, then include a wildcard when you write the condition for the path-based routing rule. Use path patterns similar to /svcA* or /svcB* to be sure that any documents on these paths are accounted for when routing requests.

Test path-based routing

To test this routing, copy the DNS name of your Application Load Balancer in a web browser and add the URL path /svcA or /svcB. When the Application Load Balancer listener receives the request, the listener forwards that request to the appropriate target group based on the path condition.

For example, suppose that the DNS name of your Application Load Balancer is alb-demo-1234567890.us-west-2.elb.amazonaws.com:

  • 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.

With path-based routing, your Application Load Balancer allows you to host multiple microservices behind a single load balancer using listener rules and target groups. Therefore, you can set up complex rules to route client requests to your applications. In addition to path-based rules, you can also route requests to particular 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 and fix failing health checks for Application Load Balancers?

Troubleshoot your Application Load Balancers

AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago