Guided Lab: Path-based routing with Application Load Balancer (AWS ALB)

Description

In this hands-on lab, you’ll learn how to configure an Application Load Balancer (ALB) to route incoming requests based on different paths. Path-based routing allows you to host multiple microservices behind a single ALB, directing traffic to the appropriate service based on the requested path.

An Application Load Balancer enables you to set up a listener with rules that direct incoming requests to target groups based on the URL. This capability is unique to Application Load Balancers and is not offered by other load balancer types like Classic Load Balancer, Network Load Balancer, and Gateway Load Balancer. The path pattern rules only apply to the path of the URL and do not consider the URL’s query parameters.

Objectives

In this lab, you will learn how to:

  • Understand the concept of path-based routing.
  • Configure an ALB to route requests based on distinct paths.
  • Test the setup using a sample HTML file.

Prerequisites

This labs assume you have basic knowledge of AWS services (EC2, VPC, ALB).

If you find any gaps in your knowledge, consider taking the following lab:

Lab Steps

Launching two EC2 Instances:
  1. Launch the first EC2 Instances using the following configurations:
    • Name: App1
    • AMI: Ubuntu Server 24.04
    • Key pair: (Create a new one.)
      • Key pair name: path-based-routing
      • Key pair type: RSA
      • Private key file format: .pem
    • Network settings
      • Allow SSH traffic from My IP
      • Allow HTTP traffic from the internet
    • Create the instance.
  2. Launch the second EC2 Instance using the following configurations:
    • Name: App2
    • AMI: Ubuntu Server 24.04
    • Key pair: Use the key pair that was created on the first instance.
    • Network settings
      • Allow SSH traffic from My IP
      • Allow HTTP traffic from the internet
Installing the NGINX web server

Note: Do these steps on both App1 and App2 instances.

  1. Connect to the server via ssh.
  2. Install the NGINX web server.
    sudo apt update
    sudo apt install nginx
  3. Once installed, open the Public IPv4 address of each instance in the browser using HTTP to verify if the NGINX has been successfully installed.
Creating a simple UI

Note: Do these steps on both App1 and App2 instances.

  1. After installing the NGINX web server, create a simple HTML.
  2. Connect again to the instance via SSH.
  3. Run the following commands:
    • For App1 instance:
      sudo vi /var/www/html/demo1.html
    • Copy and paste the HTML block below.
      <!DOCTYPE html>
      <html>
      <head>
      <title>Welcome to App1!</title>
      </head>
      <body>
      <h1>Welcome to App1!</h1>
      </body>
      </html>
    • Save the file using :wq! command.
    • For App2 instance:
      sudo vi /var/www/html/demo2.html
    • Copy and paste the HTML block below.
      <!DOCTYPE html>
      <html>
      <head>
      <title>Welcome to App2!</title>
      </head>
      <body>
      <h1>Welcome to App2!</h1>
      </body>
      </html>
    • Save the file using:wq! command.
Creating Target Groups:
  1. Target Type: Instances
  2. Set up two target groups (e.g., tg1 and tg2) with Protocol as HTTP and Port as 80.
  3. Select the existing VPC
  4. Register EC2 instances running App1 with tg1 (set HealthCheckPath as /demo1.html).
  5. Register EC2 instances running App2 with tg2 (set HealthCheckPath as /demo2.html).
Creating an Application Load Balancer
  • Load Balancer name: demo
  • Scheme: Internet-facing
  • Load Balancer IP address type: IPV4
  • Network mapping
    • Select the existing VPC
    • Mappings: Select all AZs
  • Security groups
    • Select the default and the launch-wizard-1
  • Listeners and routing
    • Protocol: HTTP
    • Port: 80
    • In the meantime, forward it to tg1 because there is no path condition upon creating the ALB.
    • Click Create Load Balancer.

Configure Listener and rules:

  • Go to the Listener details pane.
    • Change the Routing actions to Return fixed response
    • Click Save Changes.
  • Define listener rules based on path patterns:
    • Click Add rule → Next
  • Add condition
    • Rule condition type: Path
    • For requests to /demo1.html, forward to tg1.
    • For requests to /demo2.html, forward to tg2.
    • Click Next.
  • Rule Priority
    • First rule (tg1): 1
    • Second rule (tg2): 2
Test Path-Based Routing:

Access your ALB using different paths (e.g., http://your-alb-dns-name/demo1.html and http://your-alb-dns-name/demo2.html).

  • Verify that requests are routed correctly to the respective pages.

Congratulations! You’ve successfully set up path-based routing with an Application Load Balancer (ALB) on AWS. By configuring listener rules based on URL paths, you’ve efficiently directed requests to different instances behind a single ALB. Whether it’s /demo1.html/ or /demo2.html/, your ALB knows where to route the traffic.

Skip to content