API Gateway

Architecture

Deploy an ALB as a lightweight API gateway:

  • Single entry point for all API traffic
  • Route to services based on path prefixes
  • SSL termination at the load balancer
  • Health monitoring with automatic failover

When to Use

Use ALB as an API gateway when:

  • You need simple path-based routing without advanced features
  • Cost is a concern (cheaper than API Gateway)
  • You want direct integration with ECS or EC2 targets
  • You do not need request transformation or throttling

Example Configuration

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

  name    = "api-gateway"
  vpc_id  = module.vpc.vpc_id
  subnets = module.vpc.public_subnets

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

  listener_rules = [
    {
      priority   = 100
      conditions = [{ path_pattern = ["/users/*"] }]
      actions    = [{ type = "forward", target_group_arn = aws_lb_target_group.users.arn }]
    },
    {
      priority   = 200
      conditions = [{ path_pattern = ["/orders/*"] }]
      actions    = [{ type = "forward", target_group_arn = aws_lb_target_group.orders.arn }]
    },
    {
      priority   = 300
      conditions = [{ path_pattern = ["/products/*"] }]
      actions    = [{ type = "forward", target_group_arn = aws_lb_target_group.products.arn }]
    }
  ]
}

Considerations

  • ALB does not provide request throttling (use WAF rate limiting)
  • No built-in request/response transformation
  • Works well with ECS Fargate and Lambda targets