Host-Based Routing

Route requests to different target groups based on the Host header.

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

  name    = "host-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 = [{
        host_header = ["api.example.com"]
      }]
      actions = [{
        type             = "forward"
        target_group_arn = aws_lb_target_group.api.arn
      }]
    },
    {
      priority = 200
      conditions = [{
        host_header = ["app.example.com"]
      }]
      actions = [{
        type             = "forward"
        target_group_arn = aws_lb_target_group.app.arn
      }]
    },
    {
      priority = 300
      conditions = [{
        host_header = ["*.staging.example.com"]
      }]
      actions = [{
        type             = "forward"
        target_group_arn = aws_lb_target_group.staging.arn
      }]
    }
  ]
}

Key Points

  • Host header matching supports wildcards
  • Use with multi-domain SSL certificates or SNI
  • Combine with path-based routing for complex scenarios