A developer is troubleshooting a client-side real estate web application hosted on `https://listings.example.com` that sends `POST` requests to an Amazon API Gateway REST API. The API is integrated with an AWS Lambda function using a Lambda Proxy integration. Users report that search submissions fail, and the browser console displays a CORS policy error indicating that the 'Access-Control-Allow-Origin' header is missing. Which two actions must the developer take to resolve this issue? (Select TWO.)
- Configure the backend Lambda function to return the 'Access-Control-Allow-Origin' header in its JSON response headers.Cevap
- Deploy the API Gateway REST API to a stage after enabling CORS on the resource in the console.Cevap
- CConfigure a CORS policy on the Amazon S3 bucket where the client-side application is hosted.
- DUpdate the Lambda function integration to a Custom Integration and configure a response mapping template in API Gateway.
- EAttach an API Gateway resource policy that explicitly permits anonymous public access to the POST method.
Cevap
The developer must configure the backend Lambda function to return the 'Access-Control-Allow-Origin' header in its JSON response, and deploy the API Gateway REST API to a stage after enabling CORS on the resource.
The correct options are configuring the backend Lambda function to return the 'Access-Control-Allow-Origin' header in its response, and deploying the API Gateway REST API to a stage. When using Lambda Proxy integration, the backend Lambda function is responsible for returning the appropriate CORS headers in its response because API Gateway passes the response from Lambda directly without modification. Additionally, enabling CORS in the API Gateway console configures the preflight OPTIONS method, but the API must be deployed to the target stage for those settings to become active.
Adım Adım Çözüm
Anahtar Kavram
Handling CORS in Amazon API Gateway REST APIs using Lambda Proxy Integration requires both configuring the API Gateway OPTIONS method (via Enable CORS) and returning the CORS headers directly from the backend Lambda function response.