Amazon API Gateway
REST, HTTP and WebSocket APIs compared, integrations, authorizers, throttling, stages and canaries, caching, private APIs and custom domains.
Exam tasks: 2.3 (secure APIs), 2.5 (performance), 4.3 and 4.4 (modernize with APIs), 3.2
The decision: which API type gives the features you need at the lowest cost, how are callers authenticated, and how do you protect the backend from too much traffic?
API Gateway is a managed front door for APIs. It authenticates, throttles, transforms and routes calls to Lambda, HTTP backends, AWS services and private resources.
REST, HTTP and WebSocket
| REST API | HTTP API | WebSocket API | |
|---|---|---|---|
| Cost and latency | Higher | Lower and faster | Per message and connection minute |
| Authorizers | IAM, Cognito, Lambda | IAM, JWT, Lambda | IAM, Lambda |
| API keys and usage plans | Yes | No | No |
| Caching | Yes | No | No |
| WAF | Yes | No | No |
| Request validation and transformation | Yes | Limited | Route selection |
| Canary releases | Yes | No | No |
| Private API | Yes | No | No |
| Endpoint types | Edge-optimized, regional, private | Regional | Regional |
Exam signal
Need WAF, API keys with usage plans, caching or private endpoints: REST API. Need the lowest cost with JWT authorization for a simple proxy: HTTP API. Need two-way push to clients: WebSocket API.
Integrations
- Lambda proxy passes the request to the function and returns what it returns. It is the common choice.
- HTTP proxy forwards to a public HTTP endpoint. AWS service integrations call services such as SQS, Step Functions or DynamoDB directly, with no Lambda in between. Mock returns a fixed response.
- VPC link connects to private resources. REST APIs reach a Network Load Balancer, and HTTP APIs can reach an ALB, an NLB or Cloud Map services.
- REST APIs wait up to 29 seconds by default, so long work should be asynchronous: accept the request, queue it, and return a status endpoint.
Authorizers
| Type | Use it when |
|---|---|
| IAM (SigV4) | Callers already hold AWS credentials, such as other services or federated users |
| Cognito user pool | Your users sign in with Cognito and send an access or ID token |
| Lambda authorizer | You need custom logic on a token or on headers, query strings and stage variables. Results are cached, default 300 seconds |
| JWT authorizer (HTTP API) | Any OIDC provider issues the tokens |
| Resource policy | Allow or deny by source IP, VPC endpoint or account |
API keys are not authentication
An API key identifies a client for throttling and metering through a usage plan. It is not an authorization mechanism. A question that wants to restrict who can call the API needs IAM, Cognito or a Lambda authorizer.
Throttling, stages and releases
- Limits apply at the account, the stage or method and the usage plan. Calls beyond the limit get a 429, so clients should retry with backoff.
- Stages such as
devandprodhave their own settings, stage variables and logging. A change reaches a stage only after you create a deployment. - A canary release on a REST stage sends a percentage of traffic to the new deployment before you promote it.
- Logging: access logs to CloudWatch Logs or Data Firehose, execution logs for debugging, and X-Ray for traces.
Caching
- Enabled per stage, with per-method overrides. Only REST APIs have it; HTTP APIs don't.
- Choose which request parameters (headers, query strings, path) form the cache key.
- Clients can send
Cache-Control: max-age=0to bypass the cache. Require authorization for this, or anyone can force misses. - For a public, cacheable API, CloudFront in front of API Gateway gives edge caching too.
Private APIs and custom domains
- A private API is reachable only through an interface VPC endpoint for
execute-api, and a resource policy must allow that endpoint. - A custom domain name needs an ACM certificate, in us-east-1 for edge-optimized APIs and in the API's Region for regional ones, and a Route 53 alias. Mutual TLS is available on custom domains.
Scenarios
Usage plans attach throttling and quotas to API keys, giving per-client limits and usage data for billing. Keys are for metering, so keep a real authorizer too. The Lambda role and memory do not limit callers, and geographic rules cannot separate partners.
Only private REST APIs are reachable solely through a VPC endpoint, and the resource policy limits callers to that endpoint. HTTP APIs have no private option and security groups do not attach to API Gateway. An edge-optimized API is public, and WebSocket APIs are regional and public.
A stage cache returns repeated responses without invoking the backend. WebSocket changes the protocol, a longer timeout does nothing for repeated reads, and API keys control access, not caching.
Related
- Caching: API Gateway cache among the other layers.
- AWS Lambda: the usual backend and its invocation limits.
- App authentication: Cognito authorizers and JWTs.