Publishing and Protecting APIs with Amazon API Gateway
This lesson explains how API Gateway creates, publishes, secures and scales APIs, most often in front of Lambda.
Learning outcomes
After completing this lesson, you will be able to:
- Choose an API type and endpoint type.
- Choose integrations and authorisation methods.
- Explain how throttling and caching protect back ends.
- Explain how long-running work is handled asynchronously.
API and endpoint types
API Gateway offers three API types. A REST API has the full set of features, including API keys and usage plans for per-client limits, response caching, request validation, AWS WAF and private endpoints. An HTTP API is simpler, with lower latency and cost and built-in JWT authorizers, for APIs that do not need those features. A WebSocket API keeps two-way connections open, so the server can push messages to clients through the connection management API, for chat and live dashboards. The following table summarises the API types.
| API type | Features |
|---|---|
| REST | Full set: API keys, usage plans, caching, request validation, WAF, private endpoints |
| HTTP | Simpler, lower latency and cost, JWT authorizers |
| WebSocket | Two-way real-time communication; server pushes via the connection management API |
A REST API also has an endpoint type. An edge-optimized endpoint routes clients through CloudFront edge locations, which suits clients spread around the world. A Regional endpoint serves clients in the same Region, or sits behind your own CloudFront distribution. A private endpoint is reachable only from VPCs through an interface endpoint. The following table summarises the endpoint types.
| Endpoint type | For |
|---|---|
| Edge-optimized | Global clients, via CloudFront |
| Regional | Same-Region clients, or behind your own CloudFront |
| Private | VPCs only, via an interface endpoint |
Which API type?
Integrations and security
Amazon API Gateway connects an API to many kinds of back end. It integrates with Lambda functions and HTTP endpoints, and also with AWS services directly, for example sending a message to an SQS queue or writing an item to DynamoDB without any Lambda function in between, which reduces cost and latency. VPC links connect the API to private resources behind a Network Load Balancer or Application Load Balancer in a VPC.
APIs are secured in several ways: IAM authorisation for AWS principals, Amazon Cognito user pool authorizers for application users, Lambda authorizers for custom token schemes, resource policies that restrict callers by account, IP address or VPC endpoint, mutual TLS for client certificates, and AWS WAF for filtering malicious requests.
Securing and integrating APIs
Throttling, caching and timeouts
Several settings protect an API and its backend. API Gateway throttles requests at the account level, by default 10,000 requests per second with a burst of 5,000 in each Region, shared by all APIs. Finer limits can be set per stage and per method, and usage plans with API keys set limits and quotas for each client, which suits APIs offered to partners. Throttled requests receive HTTP 429.
Caching, for REST APIs, stores responses for a TTL, so repeated requests do not reach the backend, which reduces its load and latency. The integration timeout is 29 seconds by default, so work that takes longer should be made asynchronous; for Regional and private REST APIs it can be raised, at the cost of some throttle quota. The following table summarises the settings.
| Setting | Detail |
|---|---|
| Account throttle | 10,000 req/s with 5,000 burst per Region |
| Finer limits | Per stage, per method, per client via usage plans and API keys |
| Caching (REST) | Reduces back-end calls |
| Integration timeout | 29 s default; raisable for Regional and private REST APIs at the cost of throttle quota |
10,000req/s
Default account rate per Region
5,000requests
Default account burst
29s
Default integration timeout
API Gateway limits
Long-running work
API Gateway integrations must respond within a timeout, 29 seconds by default for REST APIs, and clients should not be kept waiting for long-running work in any case. Such work is handled asynchronously. The API places the request on an Amazon SQS queue or starts an AWS Step Functions execution, and returns immediately with an identifier for the job.
The client then checks the job's status later through another API call, or receives a notification when it completes, for example through a WebSocket connection, an SNS message or a callback URL. The work itself runs for as long as it needs, independently of the client's connection.
The following diagram shows the asynchronous request pattern.

A serverless API for partners
An API offered to partners must meet several requirements for control and protection. The following table identifies the API Gateway feature that meets each.
| Requirement | API Gateway feature |
|---|---|
| Each partner limited to 100 requests per second and 1 million a month | REST API usage plans with API keys |
| Reject malformed requests before they reach Lambda | Request validation |
| Protect against SQL injection | AWS WAF on the REST API |
| Cache product lookups for 5 minutes | Stage caching |
| Internal API reachable only from the company's VPC | Private REST API with a VPC endpoint |
Most of these features exist only on REST APIs. If the question asks for the cheapest, simplest API with JWT authentication and none of these features, an HTTP API is the better answer.
How should an API handle a request whose processing takes several minutes?
Show answer
Asynchronously: accept the request, start the work, for example a Step Functions execution or a queue message, and return an ID the client checks later, because the integration timeout is 29 seconds by default.
Exam Tips
Remember for the exam
- For a serverless REST API, use API Gateway with Lambda.
- For rate limits and quotas for each customer, use usage plans with API keys.
- For an API reachable only from inside VPCs, use a private API with an interface endpoint.
- To reduce the load on the back end from repeated requests, use API Gateway caching.
- When processing takes longer than the API timeout, return immediately and process asynchronously.
- When the server must push updates to browser clients, use an API Gateway WebSocket API.