Path-based Routing in AWS Application Load Balancer

Zeeshan Baig
May 6, 2019 · 5 min read
Image for post
Image for post
www.datanextsolutions.com

Overview

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.

Image for post
Image for post
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 button

Image for post
Image for post
Create 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 slides

Image for post
Image for post
Target group settings

Optionally set the Advance Health Check settings to the following

Image for post
Image for post
Health 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.

Image for post
Image for post
Adding Targets EC2 Instances to the groups
Image for post
Image for post
Assign 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 Balancer

Image for post
Image for post
Create ALB

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

Image for post
Image for post
ALB name and Ports

Select the Availability zones that your ALB would be running

Image for post
Image for post
ALB AZs

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

Image for post
Image for post
Security Warning
Image for post
Image for post
Select Security Group

Since we already created Target group select the existing target group en from the list

Image for post
Image for post
Select or Create Target Group
Image for post
Image for post
Review Target Instances
Image for post
Image for post
Review ALB settings and click Create

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

Image for post
Image for post
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 rules

Image for post
Image for post
Create 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… column

Image for post
Image for post
Creating Rules

Do the same for /en/ at the end you should have 2 rules defined as follows

Image for post
Image for post
Path-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.

Image for post
Image for post
Image for post
Image for post
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.

@IamZeeshanBaig

About DataNext

DataNext Solutions is a US based system integrator, specialized in Cloud, Big Data, DevOps technologies. As a registered AWS partner, our services comprise of any Cloud Migration, Cost optimization, Integration, Security and Managed Services. Click here and Book Free assessment call with our experts today or visit our website www.datanextsolutions.com for more info.

DataNext Solutions

Cloud | Security | DevOps | Big Data

Zeeshan Baig

Written by

Cloud Security Expert & CEO of DataNext Solutions, helping people every day with the latest tech. Connect @LinkedIn http://bit.ly/zb-linkedin

DataNext Solutions

DataNext Solutions is a US based system integrator, specialized in Cloud, Big Data, DevOps technologies. As a registered AWS partner, our services comprise any Cloud Migration, Cost optimization, Integration, Security and Managed Services.

Zeeshan Baig

Written by

Cloud Security Expert & CEO of DataNext Solutions, helping people every day with the latest tech. Connect @LinkedIn http://bit.ly/zb-linkedin

DataNext Solutions

DataNext Solutions is a US based system integrator, specialized in Cloud, Big Data, DevOps technologies. As a registered AWS partner, our services comprise any Cloud Migration, Cost optimization, Integration, Security and Managed Services.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store