A developer is designing a web application that uses Amazon Cognito User Pools for user authentication and Amazon API Gateway REST APIs for the backend. The API endpoints must be secured so that only users with an 'Active' subscription can access them. The subscription status is stored in an external Amazon DynamoDB table and updated in real-time, which prevents it from being stored as a static attribute in the Cognito ID or access tokens. Which solution should the developer implement to secure the API Gateway endpoints?
- AImplement a Cognito User Pool Authorizer on API Gateway, and configure a Cognito Post-Authentication Lambda trigger to query the DynamoDB table and dynamically inject the subscription status into the API Gateway authorizer context.
- BImplement a Cognito Identity Pool to exchange the User Pool token for temporary AWS credentials, and attach an IAM policy to the authenticated role that uses policy conditions to query the DynamoDB table in real-time.
- Implement an API Gateway Lambda Authorizer that validates the incoming Cognito token, queries the DynamoDB table to verify the user's subscription status, and returns an IAM policy to allow or deny the request.Answer
- DImplement a Cognito User Pool Authorizer on API Gateway, and configure API Gateway integration request mapping templates to query the DynamoDB table and reject unauthorized requests before they reach the backend.
Answer
Implement an API Gateway Lambda Authorizer that validates the incoming Cognito token, queries the DynamoDB table to verify the user's subscription status, and returns an IAM policy to allow or deny the request.
An API Gateway Lambda Authorizer allows custom authorization logic. In this scenario, it can parse and validate the Cognito token to authenticate the user, query DynamoDB to check the real-time subscription status, and dynamically generate an IAM policy that allows or denies access to the API resources.
Step-by-Step Solution
Key Concept
Using API Gateway Lambda Authorizers for custom, dynamic authorization checks that cannot be performed by built-in Cognito Authorizers.
Estimated Time:1m 30s