API Gateway

Difficulty: Intermediate

Question

What is Amazon API Gateway, what does it handle for you, and how do REST APIs, HTTP APIs, and WebSocket APIs differ?

Answer

Amazon API Gateway is a fully managed service that acts as the front door for your APIs: it receives HTTP or WebSocket requests, authenticates and throttles them, optionally validates or transforms them, and forwards them to a backend such as Lambda, an HTTP service, or another AWS service. It handles TLS, scaling, rate limiting, and monitoring, which is why it is the standard way to expose serverless backends. It comes in three flavours: REST APIs (most features), HTTP APIs (simpler, cheaper, lower latency), and WebSocket APIs (two-way real-time connections).

What API Gateway does on every request

  1. Terminates TLS on the default endpoint or your custom domain (certificate from ACM).
  2. Authenticates: IAM (SigV4), Amazon Cognito user pools, JWT authorizers (HTTP APIs), or a Lambda authorizer with custom logic.
  3. Applies throttling: account-level limits (by default 10,000 requests per second with a burst of 5,000, per Region) plus stage, route, or usage-plan limits. Excess requests get 429 Too Many Requests.
  4. Optionally validates the request body and parameters (REST APIs).
  5. Calls the integration: Lambda proxy, HTTP proxy, a private service through a VPC link, or an AWS service directly (for example writing to SQS or DynamoDB without a Lambda).
  6. Returns the response and records metrics in CloudWatch and access logs.

REST vs HTTP vs WebSocket APIs

FeatureREST APIHTTP APIWebSocket API
Typical usePublic APIs needing full controlMost Lambda or HTTP backendsChat, live dashboards, notifications
AuthIAM, Cognito, Lambda authorizer, API keysIAM, JWT (Cognito, Auth0), Lambda authorizerIAM, Lambda authorizer on connect
Usage plans and API keysYesNoNo
Response cachingYesNoNo
Request validation, mapping templatesYesLimited parameter mappingRoute selection expressions
AWS WAFYesNo (put CloudFront with WAF in front)No
Endpoint typesEdge-optimised, Regional, PrivateRegionalRegional
PriceHigherSignificantly lowerPer message and connection minute

Rule of thumb: start with an HTTP API, and move to a REST API only if you need a feature it lacks (usage plans for API keys, caching, WAF, private endpoints, request validation).

Common limits to know

  • Integration timeout is 29 seconds by default; for REST Regional and private APIs it can now be raised through a quota increase, but long work should be asynchronous (accept, queue, return 202).
  • Maximum payload size is 10 MB.
  • WebSocket connections last at most 2 hours and close after 10 minutes idle.

Gotchas

  • Lambda proxy integrations must return the exact response shape (statusCode, headers, body as a string), or clients see a 502 Bad Gateway.
  • CORS must be configured in API Gateway (and returned by Lambda for proxy integrations on REST APIs); missing headers are a classic frontend bug.
  • Throttling limits are shared across all APIs in an account and Region, so one noisy API can starve another. Set per-stage or per-route limits.
A request through API Gateway. API Gateway authenticates and throttles before your backend ever sees the request.
  1. Client: Mobile app calls POST /orders over HTTPS.
  2. Custom domain and TLS: api.example.com with an ACM certificate.
  3. Authorizer: JWT, Cognito, IAM, or Lambda authorizer.
  4. Throttling and validation: Rate limits, usage plans, body checks.
  5. Integration: Lambda, HTTP backend, VPC link, or SQS.

Code examples

HTTP API with a JWT authorizer using AWS SAM

Resources:
  OrdersApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      Auth:
        DefaultAuthorizer: CognitoJwt
        Authorizers:
          CognitoJwt:
            IdentitySource: $request.header.Authorization
            JwtConfiguration:
              issuer: https://cognito-idp.ap-south-1.amazonaws.com/ap-south-1_AbCdEf123
              audience: [3n4b5urk1ft4fl3mg5e62d9ado]
      DefaultRouteSettings:
        ThrottlingRateLimit: 200
        ThrottlingBurstLimit: 400

  CreateOrderFn:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: python3.13
      Handler: app.handler
      CodeUri: src/
      Events:
        CreateOrder:
          Type: HttpApi
          Properties:
            ApiId: !Ref OrdersApi
            Path: /orders
            Method: POST

API Gateway validates the Cognito JWT and enforces rate limits before invoking the Lambda function, so the function only handles authenticated, throttled traffic.

Key points

  • API Gateway is the managed front door: TLS, auth, throttling, validation, routing to backends
  • Three types: REST (full features), HTTP (cheaper, simpler), WebSocket (two-way)
  • Default account throttle is 10,000 RPS with 5,000 burst per Region; excess returns 429
  • 29-second default integration timeout and 10 MB payload limit
  • Lambda proxy responses must have statusCode, headers, and a string body

Common follow-up questions

  • When would you choose a REST API over an HTTP API?
  • How would you handle a request that takes longer than 29 seconds?
  • How does a Lambda authorizer work, and how is its result cached?
  • How would you rate-limit different customers differently?

Interview tips

  • Explain what happens on each request (auth, throttle, integrate) rather than just calling it 'a managed API service'
  • Know the REST vs HTTP API trade-off; it is the most common follow-up
  • Mention the 29-second timeout and the async pattern for long tasks; it shows production experience

Concepts covered

Amazon API Gateway, REST vs HTTP APIs, Throttling, Authorizers, Lambda Proxy Integration

More AWS & Cloud Fundamentals questions