Soru

Zorluk: KolayTroubleshooting API Gateway Errors and CORS

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.)

  1. Configure the backend Lambda function to return the 'Access-Control-Allow-Origin' header in its JSON response headers.Cevap
  2. Deploy the API Gateway REST API to a stage after enabling CORS on the resource in the console.Cevap
  3. C
    Configure a CORS policy on the Amazon S3 bucket where the client-side application is hosted.
  4. D
    Update the Lambda function integration to a Custom Integration and configure a response mapping template in API Gateway.
  5. E
    Attach 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

1
Add the required CORS headers to the Lambda function response.
The Lambda function returns a payload containing headers: { 'Access-Control-Allow-Origin': 'https://listings.example.com' }.
Since the API uses Lambda Proxy integration, API Gateway does not automatically inject CORS headers into responses from the integration; the backend function must return them.
2
Enable CORS on the REST API resource and deploy the API.
The mock integration for the preflight OPTIONS method is created and deployed to the active stage.
Before browsers send a non-simple request (like POST with JSON payload), they perform a preflight OPTIONS request. API Gateway must have CORS enabled (to handle OPTIONS) and the API must be deployed to the stage to serve these requests.

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.
Bu soruyu puanla