Path-Based Routing

Route requests to different target groups based on URL path patterns.

module "path_routing_alb" {
  source  = "registry.patterneddesigns.ca/patterneddesigns/alb/aws"
  version = "3.0.0"

  name    = "path-routing-alb"
  vpc_id  = module.vpc.vpc_id
  subnets = module.vpc.public_subnets

  https_listeners = [{
    port            = 443
    certificate_arn = aws_acm_certificate.main.arn
  }]

  listener_rules = [
    {
      priority = 100
      conditions = [{
        path_pattern = ["/api/*"]
      }]
      actions = [{
        type             = "forward"
        target_group_arn = aws_lb_target_group.api.arn
      }]
    },
    {
      priority = 200
      conditions = [{
        path_pattern = ["/admin/*"]
      }]
      actions = [{
        type             = "forward"
        target_group_arn = aws_lb_target_group.admin.arn
      }]
    },
    {
      priority = 300
      conditions = [{
        path_pattern = ["/static/*"]
      }]
      actions = [{
        type             = "forward"
        target_group_arn = aws_lb_target_group.static.arn
      }]
    }
  ]
}

Key Points

  • Lower priority numbers are evaluated first
  • Path patterns support wildcards (*)
  • Default action handles unmatched requests