Lesson 1 of 2 Amazon API Gateway and AWS Step Functions

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 typeFeatures
RESTFull set: API keys, usage plans, caching, request validation, WAF, private endpoints
HTTPSimpler, lower latency and cost, JWT authorizers
WebSocketTwo-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 typeFor
Edge-optimizedGlobal clients, via CloudFront
RegionalSame-Region clients, or behind your own CloudFront
PrivateVPCs only, via an interface endpoint
Sort into groups

Which API type?

Exam skills 2.1.5 · 2.1.1

Choose the API Gateway API type for each requirement.

Log in to try this activity Interactive activities are free with an account.

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.

Match the pairs

Securing and integrating APIs

Exam skills 1.2.3 · 2.1.5

Match each requirement with the API Gateway feature that meets it.

Log in to try this activity Interactive activities are free with an account.

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.

SettingDetail
Account throttle10,000 req/s with 5,000 burst per Region
Finer limitsPer stage, per method, per client via usage plans and API keys
Caching (REST)Reduces back-end calls
Integration timeout29 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

Fill in the value

API Gateway limits

Exam skills 4.4.6

Recall API Gateway's default limits.

Log in to try this activity Interactive activities are free with an account.

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.

Handling long-running work behind API Gateway. The API places the request on an SQS queue or starts a Step Functions execution and returns at once with a job identifier. The work runs for as long as it needs, and the client checks the job's status later or receives a notification through a WebSocket, SNS or a callback URL.
The paths are illustrative. The API responds within its timeout because it only accepts the job.
Worked example

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.

RequirementAPI Gateway feature
Each partner limited to 100 requests per second and 1 million a monthREST API usage plans with API keys
Reject malformed requests before they reach LambdaRequest validation
Protect against SQL injectionAWS WAF on the REST API
Cache product lookups for 5 minutesStage caching
Internal API reachable only from the company's VPCPrivate 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.

Check yourself

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.