Use an ALB (Application Load Balancer) for long-running containers on ECS or EC2. Use API Gateway for Lambda-based APIs, or public APIs that need managed throttling and auth.
ALB is the natural front for containers. It does:
- L7 routing: it reads the HTTP request and sends it to a target group (a set of servers or tasks) based on the path or host name.
- Health checks on each target.
- WebSockets and long-lived connections.
Its pricing works well when traffic is high and steady. Auth, rate limiting and validation stay in your NestJS code. You can optionally put AWS WAF (Web Application Firewall) in front of it.
API Gateway is a managed "front door" for your API. Without writing code, it gives you:
- Direct integration with Lambda.
- Throttling and usage plans.
- JWT, Cognito or Lambda authorizers (checks that run before your code to decide if a request is allowed).
- Request validation.
- Stages, such as
devandprod.
AWS bills it per request. This is cheap at low volume but adds up at high volume. Its default integration timeout is 29 seconds for REST APIs (30 seconds for HTTP APIs). You can raise it for some API types. This timeout and its payload size limits rule out some workloads.
In short: ALB for containerized services, API Gateway for Lambda-based or public APIs that need managed throttling and auth.
- An ALB routes by path and host to target groups, runs health checks and supports WebSockets.
- An ALB suits long-running containers and steady high traffic.
- API Gateway gives Lambda integration, throttling, usage plans, authorizers and validation without code.
- API Gateway is billed per request and has timeout and payload limits.
- Containers usually sit behind an ALB, and Lambda APIs sit behind API Gateway.