Configuring Amazon API Gateway for Serverless Applications
Amazon API Gateway is the front door of most serverless applications. Its configuration determines latency, cost, protection against overload, security and observability. This lesson explains the API types and endpoint types, stages and deployments, integrations and timeouts, throttling and usage plans, caching, authorisation and protection, and logging and monitoring.
Learning outcomes
After completing this lesson, you will be able to:
- Compare REST APIs, HTTP APIs and WebSocket APIs and the edge-optimised, Regional and private endpoint types.
- Explain stages, deployments, stage variables and custom domain names.
- Explain throttling at account, stage, method and usage plan levels, and API caching.
- Describe authorisation options, AWS WAF integration, and logging and monitoring for APIs.
API Types and Endpoint Types
API Gateway offers three API types. REST APIs have the richest feature set: request validation and transformation, API keys and usage plans, caching, canary releases, AWS WAF integration, resource policies and private endpoints. HTTP APIs are simpler, lower in latency and cheaper, with native JWT authorisers and automatic deployments, but they lack caching, usage plans, canary settings and WAF integration. WebSocket APIs maintain persistent connections for real-time, two-way communication.
REST APIs have three endpoint types. Edge-optimised endpoints route requests through the CloudFront network to the API's Region, which helps geographically distributed clients. Regional endpoints serve clients in the same Region and can be placed behind a customer-managed CloudFront distribution or used with Route 53 latency routing for multi-Region APIs. Private endpoints are reachable only from VPCs through an interface VPC endpoint, with a resource policy restricting access.
Stages, Deployments and Custom Domains
For REST APIs, changes become live only when they are deployed: a deployment is a snapshot of the API configuration, and a stage, such as dev or prod, points to a deployment and has its own settings for throttling, caching, logging, tracing and canary releases. Stage variables are name-value pairs per stage that integrations can reference, for example to invoke a different Lambda alias per stage (function:orders:${stageVariables.alias}), which lets one API definition serve several environments. HTTP APIs can deploy changes automatically to a stage.
Custom domain names map a domain such as api.example.com to stages with base path mappings, using an ACM certificate (in us-east-1 for edge-optimised endpoints, in the API's Region for Regional endpoints). With Regional custom domains in several Regions and Route 53 latency or failover records, the same domain can be served from multiple Regions.
Integrations and Timeouts
Integrations connect methods to backends. Lambda proxy integration passes the whole request to a function and returns the function's response, the most common choice. HTTP integrations call HTTP endpoints, AWS service integrations call AWS APIs directly, for example sending a message to SQS or starting a Step Functions execution without a Lambda function, and private integrations reach resources in a VPC through a VPC link to an NLB or ALB.
The default integration timeout is 29 seconds for REST and HTTP APIs. For Regional and private REST APIs, the timeout can be increased beyond 29 seconds with a quota increase, at the cost of reduced throttle limits. Long-running work is better made asynchronous: the API accepts the request, places it on a queue or starts a workflow, and returns immediately with an identifier the client can poll.
Throttling, Usage Plans and Caching
API Gateway protects backends with throttling based on a token bucket, defined by a steady-state rate and a burst. Limits apply at several levels: an account-level limit per Region (10,000 requests per second with a burst of 5,000 by default, raisable), stage and method limits that the API owner sets, and for REST APIs usage plans that set rate, burst and quotas (requests per day, week or month) per client identified by an API key. Requests over a limit receive HTTP 429 Too Many Requests, and clients should retry with backoff. API keys identify clients for usage plans; they are not an authentication mechanism on their own.
REST API caching stores responses per stage in a provisioned cache of a chosen size, with a TTL (default 300 seconds, maximum 3,600), cache keys from selected parameters and per-method overrides. Caching reduces backend load and latency for repeated requests, and cache metrics CacheHitCount and CacheMissCount show its effectiveness.
API Gateway limits
Authorisation and Protection
API Gateway supports several authorisation methods. IAM authorisation requires requests signed with AWS Signature Version 4, which suits service-to-service calls within AWS. Amazon Cognito user pool authorisers (REST) and JWT authorisers (HTTP) validate tokens from identity providers. Lambda authorisers run custom logic on tokens or request parameters and return an IAM policy, with caching of results. Resource policies on REST APIs allow or deny access by account, IP range or VPC endpoint, and mutual TLS authenticates clients with certificates. AWS WAF web ACLs on REST API stages block common attacks and rate-limit by IP address, adding protection beyond throttling.
Logging and Monitoring
API Gateway publishes CloudWatch metrics per API and stage, including Count, Latency (total time), IntegrationLatency (backend time), 4XXError and 5XXError. The difference between Latency and IntegrationLatency shows time spent in API Gateway itself, such as authorisers. Two kinds of logs are available: execution logs (REST APIs) record detailed processing steps for troubleshooting and can include request and response data, so they should be used carefully in production; access logs record one customisable line per request with context variables such as request ID, client IP, status and latency, suitable for analysis. Enabling X-Ray tracing on a stage traces requests through API Gateway into Lambda and other services.
Read API Gateway signals
Hardening a Public API
A public REST API backed by Lambda is overwhelmed by a single partner's batch job, has no visibility into which clients cause errors, and serves the same product data repeatedly.
-
Per-client limits
Issue API keys to partners and create usage plans with rate, burst and daily quotas per partner tier.
-
Protection
Attach an AWS WAF web ACL with managed rule groups and a rate-based rule per IP.
-
Caching
Enable stage caching for the product endpoints with a 10-minute TTL keyed on product ID.
-
Visibility
Enable access logs with the API key ID and status, alarms on 5XXError and Latency, and X-Ray tracing.
A client calling an HTTP API receives 429 responses. Which settings could be the cause?
Show answer
Throttling limits: the account-level limit in the Region or the route and stage throttling settings. (HTTP APIs have no usage plans, so per-client quotas would require a REST API.)
Exam Tips
Remember for the exam
- REST APIs offer caching, usage plans, API keys, canaries, WAF and private endpoints; HTTP APIs are cheaper and faster with JWT authorisers.
- Stages point to deployments and hold throttling, caching, logging and canary settings; stage variables select per-stage backends.
- Default integration timeout is 29 seconds; make long work asynchronous.
- Throttling applies at account (10,000 rps, 5,000 burst default), stage, method and usage plan levels; 429 indicates throttling.
- Latency minus IntegrationLatency is time spent in API Gateway; access logs suit analysis, execution logs suit troubleshooting.