In this tutorial , We will learn how to configure path-based routing on the Application Load balancer.

Before that , If you want to know More about Application Load balancer and How to create an Application Load balancer using AWS Console , Check this Article.

What is Path-based Routing?

Path-based routing is one of the unique feature offered by ALB.

Path-based routing is also referred as URL based routing.

The Application load balancer will forward the requests to the specific targets based on the Rules configured in the Load balancer.

How it Works?

application load balancer

Lets say you’re running Micro-services on EC2 Instances and You can direct the traffic based on the request to the specific EC2 Instances using path-based routing.

To do that , We have to create a Listener and configure the rules to forward the requests to the EC2 Instances based on the URL path.

PreRequisites

You should have a running EC2 Instances with the microservices running on it and Application load balancer is configured.

You can refer this article , To configure ALB.

  • Running EC2 Instance with Micro-services running on it.
  • Application Load balancer.

Setup Target Groups:

To setup path-based routing , We need at-least 2 Target groups.

To Create target groups , Login to EC2 Console

Under LOAD BALANCING , Click Target Groups , Select Create target group

We will use the above diagram as reference ,Such that , /product is configured on EC2 Instance1 and /services is configured on EC2 Instance2.

Lets Create a Target Group for EC2 Instance1 (/product),

Specify a name for the target group , And the Protocol and the Port be HTTP and 80 and Click Create.

application load balancer

Now We have to register the EC2 Instance1 to this target group. To do that ,

Select the TargetGroup1 , Click Targets , Choose Edit

Select the Instance and Click Add to registered and choose Save.

Following the same Process , We have to create another Target group.

application load balancer

Register the EC2Instance2 to this Targetgroup.

Now We have created 2 Target Groups required for Configuring Path based Routing.

Configuring Listener in ALB

Now We have to configure the listener with the rules to forward the requests to the Target Groups.

Under LOAD BALANCING , Choose Load balancers

Select the Application Load balancer , Choose Listeners ,

You will have the listener as shown below.

application load balancer

Select View/edit rules , To add a Rule , Click + symbol

application load balancer

And these Choose Insert Rule ,

Choose the Add Condition , Select Path , specify the path of the EC2Instance1 and then click the Tick Mark.

application load balancer

Then , We need to add the TargetGroup of the /product path , Click Add action , Choose Forward to , Select the TargetGroup1 , Click the Tick Mark.

application load balancer

And then Click Save.

Lets Add a Rule for EC2Instance2 for the path /services.

Click Insert Rule , Select Add condition , Choose Path , Specify the path and select the Tick Mark.

application load balancer

Then Click Add action , Choose Forward to , Select the TargetGroup2 , Click the tick mark

application load balancer

And finally Click Save.

Important Note:

The rules we have configured for the Path based routing are an Exact Match.

For this tutorial , We have configured rules to forward the requests from the Users to a definite Path (/product & /services).

But There are some cases where the users requests for an information which is further down the path such as /product/electronics and /services/ITconsulting.

So In this case We have to configure a rule for the path with the wildcard , This will ensure that the any other information under this path is served for the users properly.

Examples as Shown Below:

application load balancer
application load balancer

Conclusion

We have learnt How to configure a Path based Routing on the Application Load balancer.

Also We know to configure rule to forward any requests down the path by adding the Wildcard.

Hope you found it helpful , Thanks for reading my article.

Please do check out my other publications.