Amazon API Gateway with AWS Lambda authorizer, & Amazon DynamoDB for Tenant API Key Authentication

Amazon Cognito → Amazon API Gateway → Lambda authorizer → Amazon DynamoDB

Implement a secure tenant-based API key authorization system using Amazon Cognito, Amazon API Gateway, AWS Lambda Authorizer, and Amazon DynamoDB.

This pattern demonstrates how to implement a secure tenant-based API key authorization system using Amazon Cognito, Amazon API Gateway, Lambda Authorizer, and Amazon DynamoDB.
Amazon Cognito authenticates users and issues JWTs (ID tokens) containing a custom tenantId claim.
The client sends the JWT in the Authorization header. API Gateway forwards the token to the Lambda authorizer, which validates and decodes the JWT, extracts the custom:tenantId claim, and queries DynamoDB to retrieve the corresponding API key.
The authorizer returns a policy document with the usageIdentifierKey set to the API key, enabling API Gateway usage plan integration.
The API Gateway then allows or denies access to the protected endpoint based on the policy returned by the authorizer.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/apigw-apikey-tenantid-cdk

Deploy

npm installcdk deploy


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the CDK stack: cdk destroy

Created by:

Lavanya Tangutur

Lavanya Tangutur

Sr. Technical Account Manager