Throttling, CORS and error mapping
Protect capacity without assuming a hard cap
Throttling limits the rate of admitted requests and allows short bursts; API Gateway uses token-bucket-style throttling. A target rate is not a guaranteed spending or traffic cap. Account/Region, stage, route and usage-plan limits vary by API type; verify quotas before deployment. Clients receiving 429 should use bounded exponential backoff with jitter and respect Retry-After when present. Do not blindly retry mutations: a request may already have changed state. A persistent idempotency key and atomic update can protect create operations; the toy CRUD lab does not implement that guarantee.
CORS is a browser rule, not authorization
A page at https://shop.example.com fetching https://api.example.com has a different origin. A browser can send an OPTIONS preflight before a request with certain methods or headers. Configure allowed origins, methods and headers narrowly. For HTTP APIs with gateway CORS configured, API Gateway handles preflight and supplies configured CORS response headers, ignoring backend CORS headers. A $default route with authorization may need an explicitly unauthenticated OPTIONS route. REST Lambda proxy integrations instead require the backend to return appropriate CORS headers for its responses. Never use CORS as protection against non-browser clients; every protected operation requires authorization.
Map failures to useful responses
Return 400 for malformed input, 401 for missing authentication, and 403 for denied access when revealing the resource is acceptable. The bookstore deliberately uses 404 for both absent and other-owner records. A successful create returns 201, an update 200, and a delete 204 with no body. A malformed Lambda proxy response or uncaught function error can become a gateway 502. Integration timeouts can produce 504; the backend may still have processed a mutation. Log correlation IDs and sanitized details internally, not stack traces to clients. Verify timeout limits by API type rather than assuming one global number. REST stage caching may reduce backend calls but must not share private responses between users.
Estimate cost and test offline
For illustration only, at a fictional rate of $1 per million requests, 250,000 billable requests cost $0.25 for the request component. Add Lambda compute and requests, database usage, transfer, logs, applicable caching and WebSocket message/connection charges as relevant; actual API Gateway rates and billing units depend on API type and region. No live price is asserted. In the lab, test valid JSON creation, invalid input, unauthorized access, another user’s record, update and deletion. The toy model does not emulate AWS routing, real authorizers, retries, quotas or browser CORS. No account or network is needed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Throttling, CORS and error mapping — knowledge check
Reason about a fictional bookstore API without cloud access.
A client receives 429 on a GET. What is a suitable response?
Does allowing one origin in CORS prevent non-browser clients from calling a protected API?
Which response represents a successful creation in this contract?
A Lambda proxy returns a malformed response. Which gateway error can result?
At a hypothetical $1 per million requests, what is the request charge for 250,000 requests?