API Gateway serves as the essential "front door" for your serverless applications, particularly when exposing AWS Lambda functions as REST or WebSocket APIs. Its primary integration type for Lambda is the "Lambda Proxy Integration," which simplifies development by passing the entire request context directly to your Lambda function and expecting a straightforward response structure. This minimizes configuration overhead on API Gateway, letting your Lambda handle all request parsing and response formatting. For more advanced scenarios, or when integrating with non-Lambda backends (like HTTP endpoints or other AWS services), "non-proxy" integrations offer granular control over request and response mapping, transforming data before it reaches your backend or client.
Beyond simple integration, API Gateway provides crucial mechanisms for managing API traffic. Throttling is a vital feature for protecting your backend services (like Lambda functions or databases) from being overwhelmed by excessive requests, whether accidental or malicious (DDoS). You can configure global account-level limits, and more granular per-method or per-stage throttling rules. This ensures service availability and predictable performance, preventing individual endpoints from consuming too many resources and impacting other parts of your application or incurring unexpected costs.
Finally, robust authorization is paramount for securing your APIs. API Gateway offers several powerful options: AWS IAM for granting access to trusted AWS principals using roles and policies; Lambda Authorizers (formerly Custom Authorizers) for highly flexible, custom authentication and authorization logic (e.g., validating JWTs from external identity providers, or implementing complex policy lookups); and Amazon Cognito User Pools, which provide managed user authentication and authorization, simplifying the integration of standard OIDC tokens for user-facing applications. Choosing the right authorization method depends on your specific security requirements and existing identity management systems.
Key Takeaways
- API Gateway acts as the scalable HTTP interface for your Lambda functions and other backends.
- Lambda Proxy integration is the simplest and most common way to integrate API Gateway with Lambda.
- Throttling prevents backend overload, ensures API stability, and helps manage costs by limiting request rates.
- API Gateway offers flexible authorization options (IAM, Lambda Authorizers, Cognito) to secure your endpoints based on your use case.
Code Example
functions:
myProtectedLambda:
handler: handler.myProtectedFunction
events:
- http:
path: /protected-resource
method: GET
cors: true
# Throttling limits for this method
request:
throttle:
rateLimit: 5 # requests per second
burstLimit: 10 # burst capacity
# Authorization using a custom Lambda Authorizer
authorizer:
name: customAuthorizer
arn: arn:aws:lambda:REGION:ACCOUNT_ID:function:MyAuthorizerFunction
type: request # or token
identitySource: method.request.header.Authorization
resultTtlInSeconds: 300 # cache authorization decisionsHow this code works
This code defines a serverless Lambda function, myProtectedLambda, and exposes it as an API endpoint, GET /protected-resource, with built-in protections for rate limiting and custom authorization. The cors: true setting ensures the API can be called from different web domains.
The request block, specifically throttle, is crucial for managing traffic. rateLimit: 5 permits only five requests per second to this endpoint, while burstLimit: 10 allows for temporary spikes in traffic, preventing the API from being overloaded and maintaining stability. Following this, the authorizer block enforces access control. It specifies a custom Lambda function, MyAuthorizerFunction, to validate incoming requests. The identitySource: method.request.header.Authorization tells API Gateway where to find the authorization token (in the Authorization header). A subtle but important detail is type: request, which means the entire request context (headers, query strings, etc.) is sent to the authorizer, not just a single token. This provides immense flexibility for complex authorization logic, unlike type: token which is more restrictive. Finally, resultTtlInSeconds: 300 caches authorization decisions for 5 minutes, significantly reducing the authorizer's invocation count and improving performance and cost efficiency.