Application Load balancer

AWS offers 3 types of load balancers as part of Elastic Load Balancer (ELB) service one is called Classic Load Balancer, TCP load balancer and the latest is the Application load balancer (ALB).

ALB offers some unique features over Classic ELB and one of the features is Path-based Routing. What is Path-based Routing? well in simple terms the ALB could forward the incoming requests to different destinations based on the path as shown in the slide.Path-based routing example

Example

In the above slide, if the request contains the text /en/ the request would be forwarded to EC2 instance where our app (Hello World) is running in English and if the request contains the text /es/, then the request would be redirected to other EC2 instance and we will see the Hello World message in Spanish.

Note: In this post Setup of EC2 is not covered.

Creating a Target Group

The first step is to set up the target groups, you need at least 2 target group to configure Path-based routing.

To start things click on the Target Groups under EC2 Instances We have 2 EC2 Instances running, click Target Groups link to start

Click on Create target group buttonCreate a Target Group

In my example, I have set up 2 target groups one is called ‘en’ which routes to /en/ and other is ‘es’ which routes to /es/ as shown in the following slidesTarget group settings

Optionally set the Advance Health Check settings to the followingHealth check settings

Adding EC2 Instances to Target Group

Next step is to add EC2 instances to the target groups. Select the target group switch to the Targets tab and click the Edit button.Adding Targets EC2 Instances to the groupsAssign your instances to the group

Create an Application Load Balancer

Now the main thing, we need to create the Application Load Balancer.Click on the EC2 instances and then Elastic Load Balancer link

Click Create under Application Load BalancerCreate ALB

Name your load balancer and add ports that your ALB would be listening on ALB name and Ports

Select the Availability zones that your ALB would be running ALB AZs

Select Security Group for ALB, make sure you allow ports that ALB is listening and forwarding onSecurity WarningSelect Security Group

Since we already created Target group select the existing target group enfrom the listSelect or Create Target GroupReview Target Instances Review ALB settings and click Create

Once created wait for few minutes to change the ALB status to Active ALB status

Add Forwarding Rules

Now we have created our target groups and ALB. Now it’s time to add the Path based rules to our Application Load Balancer.

Click the ALB name, then select the Listeners tab and under Listener click View/edit rulesCreate or Edit ALB rules

Click the + sign on top and click Insert Rule then select the Rule type as Path, enter /es/ in the is and select a target group es in the Forward to…columnCreating Rules

Do the same for /en/ at the end you should have 2 rules defined as followsPath-based rules

Test

Once we have these rules created, copy the DNS name of the ALB and enter in the browser with /en/ and /es/ in the URL you should see the different sites accordingly.Test Results

P.S. Spanish translation is courtesy of Google Translate.

Hopefully, this post helps you to understand how to add Path-based routing to AWS Application Load Balancer. Feel free to comment for any questions I would be happy to help.

Last updated