How to Create an ALB Listener with Multiple Path Conditions Using Terraform


When designing modern cloud-native applications, it’s common to host multiple services under a single domain. Application Load Balancers (ALBs) in AWS provide an efficient way to route traffic to different backend services based on URL path conditions. This article will guide you through creating an ALB listener with multiple path-based routing conditions using Terraform, assuming you already have SSL configured.

Prerequisites

  • AWS Account: Ensure you have access to an AWS account with the necessary permissions to create and manage ALB, EC2 instances, and other AWS resources.
  • Terraform Installed: Terraform should be installed and configured on your machine.
  • SSL Certificate: You should already have an SSL certificate set up and associated with your ALB, as this guide focuses on creating path-based routing rules.

Step 1: Set Up Path-Based Target Groups

Before configuring the ALB listener rules, you need to create target groups for the different services that will handle requests based on the URL paths.

resource "aws_lb_target_group" "service1_target_group" {
  name     = "service1-tg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.main_vpc.id
}

resource "aws_lb_target_group" "service2_target_group" {
  name     = "service2-tg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.main_vpc.id
}

In this example, we’ve created two target groups: one for service1 and another for service2. These groups will handle the traffic based on specific URL paths.

Step 2: Create the HTTPS Listener

Since we’re focusing on path-based routing, we’ll configure an HTTPS listener that listens on port 443 and uses the SSL certificate you’ve already set up.

resource "aws_lb_listener" "https_listener" {
  load_balancer_arn = aws_lb.my_alb.arn
  port              = "443"
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-2016-08"
  certificate_arn   = aws_acm_certificate.cert.arn

  default_action {
    type = "fixed-response"
    fixed_response {
      content_type = "text/plain"
      message_body = "404: Not Found"
      status_code  = "404"
    }
  }
}

Step 3: Define Path-Based Routing Rules

Now that the HTTPS listener is set up, you can define listener rules that route traffic to different target groups based on URL paths.

resource "aws_lb_listener_rule" "path_condition_rule_service1" {
  listener_arn = aws_lb_listener.https_listener.arn
  priority     = 1

  action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.service1_target_group.arn
  }

  condition {
    path_pattern {
      values = ["/service1/*"]
    }
  }
}

resource "aws_lb_listener_rule" "path_condition_rule_service2" {
  listener_arn = aws_lb_listener.https_listener.arn
  priority     = 2

  action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.service2_target_group.arn
  }

  condition {
    path_pattern {
      values = ["/service2/*"]
    }
  }
}

In this configuration:

  • The first rule routes traffic with paths matching /service1/* to service1_target_group.
  • The second rule routes traffic with paths matching /service2/* to service2_target_group.

The priority field ensures that Terraform processes these rules in the correct order, with lower numbers being processed first.

Step 4: Apply Your Terraform Configuration

After defining your Terraform configuration, apply the changes to deploy the ALB with path-based routing.

  1. Initialize Terraform:
   terraform init
  1. Review the Plan:
   terraform plan
  1. Apply the Configuration:
   terraform apply

Conclusion

By leveraging path-based routing, you can efficiently manage traffic to different services under a single domain, improving the organization and scalability of your application architecture.

This approach is especially useful in microservices architectures, where different services can be accessed via specific URL paths, all secured under a single SSL certificate. Path-based routing is a powerful tool for ensuring that your ALB efficiently directs traffic to the correct backend services, enhancing both performance and security.