A developer is building a serverless REST API using Amazon API Gateway and an AWS Lambda backend with Lambda proxy integration. The API will be accessed by a web application hosted on a different domain. The developer needs to secure the API using an existing Amazon Cognito User Pool and ensure that the web application can successfully make cross-origin requests. Which two actions should the developer take to meet these requirements? (Choose two.)
- Configure a Cognito User Pool authorizer in API Gateway and apply it to the API methods.Answer
- Enable CORS on the API Gateway resource to handle preflight OPTIONS requests, and program the Lambda function to include the Access-Control-Allow-Origin header in its response object.Answer
- CCreate a custom Lambda authorizer that validates the client's credentials against the Cognito User Pool and returns an IAM policy.
- DEnable CORS in the API Gateway console for the resource, which automatically injects the Access-Control-Allow-Origin header into the response of the Lambda proxy integration.
- EDefine an API Gateway integration response mapping template to inject the Access-Control-Allow-Origin header into the response payload received from the Lambda function.
Answer
To meet the requirements, the developer must configure a native Cognito User Pool authorizer in API Gateway to secure the API methods. To support CORS in a Lambda proxy integration, the developer must enable CORS on the API Gateway resource to handle preflight OPTIONS requests, and also ensure the backend Lambda function returns the Access-Control-Allow-Origin header in its JSON response object.
To secure the API with Cognito, the developer should configure a native Cognito User Pool authorizer, which natively validates JWTs without needing custom Lambda authorizer code. To allow cross-origin requests in a Lambda proxy integration, the developer must enable CORS on the resource to handle the OPTIONS preflight requests, and the Lambda function itself must return the Access-Control-Allow-Origin header, because API Gateway does not modify headers in proxy integrations.
Step-by-Step Solution
Key Concept
Configuring security authorization and cross-origin resource sharing (CORS) within Amazon API Gateway REST APIs using Lambda proxy integration.
Estimated Time:2m 0s