Amazon API Gateway
API Gateway as the serverless front door for APIs, REST vs HTTP APIs, integrations with Lambda, HTTP and AWS services, endpoint types, security, throttling, caching and limits.
What it is
Amazon API Gateway is a fully managed service to create, publish and secure APIs. Lambda + API Gateway gives a REST API with no infrastructure to manage.
- Supports REST and WebSocket APIs (and the lighter HTTP APIs).
- Handles API versioning (v1, v2…) and different environments (dev, test, prod) with stages.
- Handles security (authentication and authorization).
- Creates API keys and handles request throttling (usage plans).
- Swagger / OpenAPI import to define APIs quickly.
- Transforms and validates requests and responses.
- Generates SDKs and API specifications.
- Caches API responses (REST APIs: TTL 300 seconds by default, up to 3,600).
REST APIs vs HTTP APIs
- REST APIs: the full feature set: API keys and usage plans, per-client throttling, request validation, caching, AWS WAF, private endpoints, edge-optimized endpoints.
- HTTP APIs: simpler, cheaper and lower latency, with JWT authorizers; Regional only, without the advanced features above.
Integrations
- Lambda function: invoke a Lambda function. The standard way to expose a REST API backed by Lambda (client → API Gateway → Lambda → DynamoDB).
- HTTP: expose HTTP endpoints in the backend, such as an internal HTTP API on premises or an Application Load Balancer. Why? To add rate limiting, caching, user authentication, API keys and more in front of it.
- AWS service: expose any AWS API through API Gateway, with no code in between. Examples: start a Step Functions workflow, post a message to SQS, or send records to Kinesis Data Streams (which can feed Data Firehose and S3). Why? To add authentication, public access and rate control.
Endpoint types
- Edge-optimized (default for REST APIs): for global clients. Requests are routed through CloudFront edge locations (better latency), but the API Gateway still lives in only one Region.
- Regional: for clients in the same Region. It can be combined with a customer-managed CloudFront distribution for more control over caching and distribution.
- Private: can only be accessed from the customer's VPC, through an interface VPC endpoint (ENI). Use a resource policy to define access.
Security
- User authentication through:
- IAM roles and policies: useful for internal applications and AWS clients (requests signed with SigV4);
- Cognito user pools: identity for external users, for example mobile app users;
- Lambda (custom) authorizers: custom logic, such as checking a third-party token;
- JWT authorizers (HTTP APIs).
- Resource policies (REST APIs) allow or deny by account, IP range or VPC endpoint; AWS WAF protects REST APIs from common web attacks.
- Custom domain name with HTTPS, through AWS Certificate Manager (ACM):
- with an edge-optimized endpoint, the certificate must be in us-east-1;
- with a Regional endpoint, the certificate must be in the API Gateway's Region;
- a CNAME or alias record must be set up in Route 53.
Limits and throttling
- Throttling: by default 10,000 requests per second per account per Region across all APIs, with bursts up to 5,000. Extra requests get 429 Too Many Requests. Usage plans set per-client limits.
- Integration timeout: 29 seconds by default (for Regional and private REST APIs it can now be raised). A Lambda function behind a synchronous API should finish well within it.
- Payload size: 10 MB.
Real-world examples
- A mobile app calls a REST API on API Gateway, with Cognito user pools for sign-in and Lambda + DynamoDB behind it.
- A company puts API Gateway in front of a legacy on-premises HTTP API to add API keys, throttling and caching without changing it.
- A clickstream is sent straight from browsers to API Gateway, which writes to Kinesis Data Streams with no Lambda in between.
Exam tips
- "Serverless REST API" → API Gateway + Lambda.
- "Add API keys, throttling, caching, authentication in front of an existing HTTP backend" → API Gateway HTTP integration.
- "Send requests straight to SQS, Kinesis or Step Functions without code" → API Gateway AWS service integration.
- "Users around the world" → edge-optimized; "same Region" → Regional; "only from inside the VPC" → private endpoint.
- "Authenticate mobile app users" → Cognito user pools; "internal AWS clients" → IAM; "custom token logic" → Lambda authorizer.
- "Custom domain on an edge-optimized API" → ACM certificate in us-east-1.
- "API returns 429" → throttling limits; "504 after 29 seconds" → integration timeout.
Common traps
- "Edge-optimized means the API runs in every Region." It runs in one Region; only the entry point is at the edge.
- "ACM certificate in the API's Region for an edge-optimized custom domain." It must be in us-east-1.
- "API Gateway can wait 15 minutes for a Lambda function." The integration timeout is 29 seconds by default.
Hands-on lab: a serverless API
Goal: create an HTTP API that calls a Lambda function.
Costs money
HTTP APIs cost about $1 per million requests; a few test calls and Lambda invocations cost far less than a cent. Any Region works.
Create the function
Lambda → Create function: name saa-hello-api, runtime Python. Code:
import json
def lambda_handler(event, context):
return {"statusCode": 200, "body": json.dumps({"message": "Hello from Lambda"})}Deploy.
Create the API
API Gateway → Create API → HTTP API → Build. Add integration Lambda → saa-hello-api, API name saa-api. Route: GET /hello. Keep the $default stage with auto-deploy. Create.
Call it
Copy the Invoke URL and open <invoke-url>/hello in a browser: {"message": "Hello from Lambda"}.
Expected result: a public HTTPS API backed by a function, with no servers.
Clean up: delete the saa-api API and the saa-hello-api function (and its log group).
AWS documentation
Lambda networking and RDS
Where Lambda runs by default, connecting Lambda to a VPC, using RDS Proxy with Lambda, invoking Lambda from RDS and Aurora, and RDS event notifications.
AWS Amplify and Device Farm
AWS Amplify for building and hosting full-stack web and mobile apps, and AWS Device Farm for testing apps on real phones and tablets.