Implementing Custom Authorizers with AWS Lambda and Amazon API Gateway
Instruction: Explain the process of implementing custom authorizers in Amazon API Gateway using AWS Lambda.
Updated
Official answer
First choose the API type. REST API Lambda authorizers use TOKEN or REQUEST inputs and return a principal identifier with an IAM policy. HTTP API Lambda authorizers have explicit payload versions; version 2.0 can use a simple authorization response when enabled, or an IAM policy. Match the response to that configuration.
Validate the credential with a maintained library or trusted identity-provider flow. For JWTs, decoding alone is insufficient: verify the signature with trusted keys and an allowed algorithm, expected issuer/audience, expiry and required claims. Then authorize the requested action and resource for the caller and tenant. Reject missing or invalid credentials and fail closed on validation failures.
Configure the identity sources and permission for API Gateway to invoke the authorizer. Treat caching as a security decision. Every input that can change the authorization result must be represented in the cache design, or caching must be disabled. In HTTP APIs, add the route key to identity sources when a cached result must differ by route; a cached simple allow can otherwise apply to other routes. A cached REST policy must cover only the resources actually permitted for that cached identity.
Test valid, expired, wrong-audience and malformed tokens, cross-tenant access, route changes, revocation delay and key rotation. Never log complete bearer tokens. Keep backend object-level authorization where needed.
REST authorizers, HTTP authorizer contracts and caching, JWT verification