APIs with API Gateway

Expose Lambda and other backends as secure HTTP APIs with API Gateway, including auth, throttling, CORS and custom domains.

Intermediate · about 4 hours · difficulty 3/5

Learn first: AWS Lambda Functions, NoSQL with DynamoDB

Amazon API Gateway is the managed front door for serverless APIs. It handles routing, authentication, throttling and TLS so your functions only contain business logic. Azure API Management and Google API Gateway / Apigee are the equivalents.

API types

  • HTTP APIs: cheaper and faster, cover most needs (Lambda and HTTP integrations, JWT authorizers, CORS). Start here.
  • REST APIs: more features such as API keys with usage plans, request validation, caching, WAF integration and private APIs.
  • WebSocket APIs: for real-time apps like chat or live quiz scores.
  • Simple single-function endpoints can use Lambda function URLs with no gateway at all.

Core concepts

  • Routes (GET /courses/{id}) map to integrations (Lambda, HTTP backend, AWS service).
  • Stages (dev, prod) with stage variables.
  • Authorizers: JWT (Cognito, Auth0, Firebase), Lambda authorizers for custom logic, or IAM auth.
  • Throttling limits requests per second to protect backends.
  • Custom domains with ACM certificates.

Auth with Cognito

Amazon Cognito user pools provide sign-up, login and JWT tokens, and plug straight into an API Gateway JWT authorizer.

Pitfalls

  • CORS errors because the preflight OPTIONS route or allowed origins are missing.
  • No throttling, letting one buggy client run up your Lambda bill.
  • Returning raw stack traces in error responses.

You are ready to move on when you can build a CRUD HTTP API on Lambda and DynamoDB protected by a JWT authorizer.

Key concepts

  • HTTP vs REST vs WebSocket APIs
  • Routes, integrations and stages
  • JWT and Lambda authorizers
  • Cognito user pools
  • Throttling and usage plans
  • CORS
  • Custom domains
  • Lambda function URLs

Mini project

Build a serverless notes API (create, list, get, delete) with an HTTP API, Lambda and DynamoDB, protected by a Cognito JWT authorizer, and test it with curl or Postman. Set a low throttling limit so a bug cannot run up costs, keep traffic tiny to stay within free tier, and tear everything down after.

Check yourself

When would you choose a REST API over an HTTP API in API Gateway?

Free resources