Part 2 of the Serverless series.

Amazon API Gateway turns HTTP requests into events for your backend. It handles TLS, routing, authentication, throttling and request logging, so your Lambda functions only deal with business logic.

Three kinds of API

API Gateway is really three products under one name:

HTTP API REST API WebSocket API
Best for Most new Lambda-backed APIs APIs needing advanced features Two-way, real-time connections
Price Lowest Higher Per message and connection minute
Auth JWT (Cognito, any OIDC), IAM, Lambda authorisers IAM, Cognito, Lambda authorisers, API keys IAM, Lambda authorisers
Extras Simple, fast, automatic CORS Usage plans, request validation, caching, AWS WAF, private APIs, mapping templates Route on a field in the message

Start with an HTTP API unless you need something only REST APIs offer, such as usage plans for third-party API keys, AWS WAF in front of the API, or private APIs inside a VPC.

For real-time features, also consider AppSync Events. My first multiplayer game, QuizDash, used a WebSocket API and had to track connections and fan out messages itself. Its successor, Trivolt, uses AppSync Events and needs none of that code.

Integrations

An API routes requests to an integration:

  • Lambda proxy: the whole request goes to a function as an event. The most common setup.
  • HTTP: forward to another HTTP endpoint.
  • AWS service integrations: call services such as SQS, EventBridge or Step Functions directly, with no Lambda in between. Great for “accept the request and queue it” endpoints.

Securing an API

  • JWT authorisers (HTTP APIs) validate tokens from Amazon Cognito or any OIDC provider without writing code.
  • Lambda authorisers run your own logic and can cache the result.
  • IAM authorisation for service-to-service calls signed with AWS credentials.
  • Throttling limits requests per second at the stage or route. Set it; the defaults are generous and your downstream database may not be.

API keys on REST APIs are for identifying and metering clients with usage plans, not for authentication.

Hands-on: an HTTP API with SAM

Resources:
  ItemsApi:
    Type: AWS::Serverless::HttpApi

  GetItemFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: nodejs22.x
      Architectures: [arm64]
      Handler: index.handler
      CodeUri: src/
      Events:
        GetItem:
          Type: HttpApi
          Properties:
            ApiId: !Ref ItemsApi
            Path: /items/{id}
            Method: GET

The handler reads event.pathParameters.id and returns { statusCode, headers, body }.

Do you need API Gateway at all?

A Lambda function URL gives a single function its own HTTPS endpoint, free. It’s ideal for webhooks, internal tools and single-function backends. Function URLs also support response streaming, useful for streaming generative AI responses. Reach for API Gateway when you want routing across functions, JWT authorisers, throttling per route, or custom domains managed in one place.

Gotchas

  • Integration timeouts. API Gateway waits only a limited time for the backend (29 seconds by default). Long-running work belongs behind a queue or workflow, with the client polling or receiving a push update.
  • Serve the API from your site’s domain. Routing /api/* through CloudFront to API Gateway (as Trivolt does) avoids CORS entirely and lets the browser treat the API as same-origin.
  • Turn on access logging. Without it, debugging 4xx and 5xx responses is guesswork.

Next: DynamoDB: a database designed around access patterns.

← Series overview