A web application hosted on a private domain attempts to submit a `PUT` request to a backend API exposed via Amazon API Gateway using a Lambda Proxy integration. The web browser blocks the request and outputs a console error indicating that the CORS preflight request failed because the 'Access-Control-Allow-Origin' header is missing. Which steps should the developer perform to resolve this issue? (Select TWO.)
- Define an OPTIONS method for the API Gateway resource that returns the required 'Access-Control-Allow-Origin' header.Answer
- Modify the Lambda function response object to include the 'Access-Control-Allow-Origin' header in its `headers` dictionary.Answer
- CUpdate the CORS configuration policy of the S3 bucket where the client-side code is stored to allow outgoing requests.
- DAdd the 'Access-Control-Allow-Origin' header to the request headers payload inside the client application code.
- EConfigure the Lambda integration as a Custom Integration rather than a Proxy Integration to enable automatic header mapping.
Answer
Define an OPTIONS method for the API Gateway resource that returns the required 'Access-Control-Allow-Origin' header, and modify the Lambda function response object to include the 'Access-Control-Allow-Origin' header in its headers dictionary.
To fix a CORS error in API Gateway when using Lambda Proxy integration, two separate adjustments are needed. First, the preflight OPTIONS request must be enabled on the API Gateway resource to respond with the 'Access-Control-Allow-Origin' header. Second, the backend Lambda function must return the 'Access-Control-Allow-Origin' header in its response JSON, because API Gateway does not inject headers into proxy responses.
Step-by-Step Solution
Key Concept
Handling CORS in API Gateway with Lambda Proxy Integration
Estimated Time:1m 30s