SkillSprout
Chapter 8 · Lesson 2
Intermediate
8 min read

API Gateway

Overview

Amazon API Gateway is a fully managed service for creating, publishing, securing and monitoring APIs at any scale. It acts as the "front door" for your backend: client requests hit API Gateway, which authenticates them, applies throttling and caching, transforms the request if needed and forwards it to a Lambda function, HTTP endpoint, VPC resource or other AWS service.

API Gateway supports three API types: REST APIs (full-featured), HTTP APIs (cheaper, faster and simpler) and WebSocket APIs (two-way, real-time communication such as chat). It is serverless: it scales automatically, charges per request and spans multiple AZs by default.

In the exam, API Gateway appears in serverless designs ("Lambda behind an API"), and for questions on throttling, caching, authorisation (IAM, Cognito, Lambda authorizers), endpoint types and protection with WAF or usage plans.

Key Concepts

  • API types: REST (caching, usage plans, API keys, WAF, request validation, private APIs), HTTP (cheaper, lower latency, JWT authorizers) and WebSocket.
  • Endpoint types: Edge-optimized (CloudFront network, global clients), Regional (same-Region clients, or your own CloudFront) and Private (only from a VPC via an interface endpoint).
  • Integrations: Lambda (proxy or custom), HTTP/HTTP proxy, AWS service (e.g. DynamoDB, SQS, Step Functions), VPC Link (private ALB/NLB) and Mock.
  • Authorisation: IAM (SigV4), Cognito user pool authorizer, Lambda (custom) authorizer, JWT authorizer (HTTP API), plus API keys for identification only.
  • Throttling: default 10,000 requests/second steady and 5,000 burst per account per Region; exceeding it returns HTTP 429; per-stage/method limits and usage plans refine it.
  • Caching (REST only): 0.5 GB to 237 GB, default TTL 300 s (max 3,600 s), per stage; cuts backend calls and latency.
  • Limits: integration timeout 29 seconds by default (REST regional/private can request more), payload 10 MB, WebSocket connection up to 2 hours idle 10 min.
  • Stages and deployments (dev/prod), canary releases, request/response mapping templates (VTL), CORS, custom domains with ACM certificates, and logging to CloudWatch.

Detailed Notes

REST vs HTTP vs WebSocket

FeatureREST APIHTTP APIWebSocket API
CostHigherUp to ~70% cheaperPer message + connection minute
LatencyHigherLowerReal-time
CachingYesNoNo
Usage plans / API keysYesNoNo
WAF integrationYesNoNo
Private API (VPC only)YesNoNo
Request validation / mapping templatesYesLimitedRoute selection
AuthIAM, Cognito, LambdaIAM, JWT, LambdaIAM, Lambda
Use forFull API managementSimple Lambda/HTTP proxiesChat, dashboards, live feeds

Endpoint types

  • Edge-optimized (default for REST): requests enter through the nearest CloudFront edge location and travel on the AWS backbone, best for globally distributed clients.
  • Regional: for clients in the same Region, or when you add your own CloudFront distribution (to control caching and WAF).
  • Private: reachable only through an interface VPC endpoint (PrivateLink); a resource policy restricts access.

Security layers

MechanismUse
IAM authorizationInternal AWS callers; SigV4 signed requests
Cognito authorizerApp users in a Cognito user pool; validates JWT automatically
Lambda authorizerCustom logic, third-party tokens (OAuth, SAML), returns an IAM policy, result cached up to 1 hour
Resource policyAllow/deny by source IP, VPC endpoint or account
AWS WAFBlock SQL injection, XSS, bots, rate limit (REST APIs)
Usage plans + API keysMeter and throttle third-party consumers (not a security mechanism)
Mutual TLSClient certificate authentication on custom domains

Throttling and caching

  • Account default: 10,000 rps steady state with 5,000 burst (token bucket). Clients exceeding limits receive 429 Too Many Requests and should retry with backoff.
  • Set stage/method-level throttling and usage plans per customer tier (for example Gold: 1,000 rps, 1 M requests/month).
  • Stage cache stores backend responses for a TTL (300 s default), keyed by method, path and chosen parameters; clients with permission can bypass it with the Cache-Control: max-age=0 header. API Gateway caching is not available on HTTP APIs.

Integration patterns

  • Lambda proxy integration passes the full request to Lambda and expects a structured response, the simplest and most common pattern.
  • Direct AWS service integration (for example API -> SQS or DynamoDB) removes the need for a Lambda function and uses mapping templates and an IAM role.
  • VPC Link exposes private ALB/NLB or on-premises resources through the API.
  • Mock integration returns canned responses for testing.
yaml
# AWS SAM: Lambda behind an HTTP API with a JWT authorizer
Resources:
  Api:
    Type: AWS::Serverless::HttpApi
    Properties:
      Auth:
        DefaultAuthorizer: CognitoJwt
        Authorizers:
          CognitoJwt:
            JwtConfiguration:
              issuer: https://cognito-idp.us-east-1.amazonaws.com/us-east-1_EXAMPLE
              audience: [my-app-client-id]
            IdentitySource: $request.header.Authorization
  OrdersFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: python3.12
      Handler: app.handler
      Events:
        GetOrders:
          Type: HttpApi
          Properties: { Path: /orders, Method: GET, ApiId: !Ref Api }

Deployments, stages and monitoring

An API is deployed to a stage (dev, test, prod) with stage variables. Canary releases send a percentage of traffic to a new deployment. Enable CloudWatch Logs and metrics (4XX, 5XX, Latency, IntegrationLatency, CacheHitCount), X-Ray tracing and access logging. Use custom domain names with ACM certificates and Route 53 aliases.

Real World Example

Scenario — a retail company exposes order and catalogue APIs to its mobile app and partners.

The mobile app needs authenticated, low-latency access worldwide; partners need metered access with different quotas; attackers sometimes hammer the login endpoints.

The team creates a REST API (edge-optimized) with a Cognito user pool authorizer for customers and API keys with usage plans for partners (Silver: 100 rps, Gold: 1,000 rps). AWS WAF blocks SQL injection and rate-limits abusive IPs. The catalogue GET /products method uses stage caching with a 5-minute TTL, slashing Lambda invocations and DynamoDB reads, while POST /orders goes directly into an SQS queue via an AWS service integration for resilience during flash sales. Canary deployments send 5% of traffic to each new release.

The result is a scalable, secure, pay-per-request front end with no servers to manage.

Architecture Diagram

API Gateway as the serverless front door
Architecture
AWS Cloud
Mobile & web clients
AWS WAF
Region
API Gatewaystage cache, throttling
Cognito user pool
LambdaGET /products
DynamoDB
SQSPOST /orders (direct)
Order processor

Clients are authenticated, throttled and cached by API Gateway before reaching backends.

  1. 1Clients call the API over HTTPS; AWS WAF filters malicious requests before they reach API Gateway.
  2. 2API Gateway validates the JWT with the Cognito authorizer (or checks the API key and usage plan for partners).
  3. 3Read requests are answered from the stage cache or forwarded to Lambda, which queries DynamoDB.
  4. 4Write requests are sent straight to SQS through a service integration and processed asynchronously.

Best Practices

  • ✓Choose HTTP APIs for simple Lambda or HTTP proxies to save cost and latency; choose REST APIs when you need caching, WAF, usage plans or private APIs.
  • ✓Authenticate every route: Cognito/JWT for users, IAM for internal services, Lambda authorizers for custom tokens.
  • ✓Protect with AWS WAF and configure throttling per stage, method and usage plan; do not rely on API keys as security.
  • ✓Enable stage caching for read-heavy, rarely changing data and set sensible TTLs and cache keys.
  • ✓Use service integrations (SQS, Step Functions, DynamoDB) to decouple and skip needless Lambda functions.
  • ✓Keep backend work under the 29-second integration timeout; for long jobs return 202 and process asynchronously.
  • ✓Use stages, canary deployments and infrastructure as code (SAM, CDK, CloudFormation) with CloudWatch alarms for 5XX errors.
  • ✓Use custom domains with ACM, enable CORS correctly, and turn on access logs and X-Ray for observability.

Common Interview Questions

Exam Tips

SAA-C03 focus

  • 29-second integration timeout and 10 MB payload limit are classic distractors; long tasks should be asynchronous.
  • "Reduce backend load / latency for repeated GETs" -> API Gateway caching (REST APIs only; TTL 300 s default).
  • "Different quotas for different customers / meter API usage" -> usage plans with API keys (REST). API keys are not authentication.
  • "Authenticate mobile app users" -> Cognito authorizer; "third-party token or custom logic" -> Lambda authorizer; "AWS caller" -> IAM.
  • "Block SQL injection / XSS / IP rate limiting" -> AWS WAF on REST API (HTTP APIs do not support WAF directly).
  • "Cheapest, simplest Lambda proxy" -> HTTP API; "two-way real-time chat" -> WebSocket API.
  • "API only from within VPC" -> Private endpoint + interface VPC endpoint; 429 = throttled, 504 = integration timeout, 403 = authorisation failure.
  • Global clients -> edge-optimized endpoint; clients in same Region or using your own CloudFront -> Regional.

Finished this lesson?

0 of 64 lessons done · 0% of the course