Soru

Zorluk: OrtaTroubleshooting API Gateway Errors and CORS

A developer is troubleshooting a mobile web application hosted on https://cargo.freight-flow.io that interacts with a backend REST API. The API is hosted on Amazon API Gateway and routes requests to an AWS Lambda function using a Lambda Proxy Integration. When the application sends a POST request to create a shipment, the browser console displays a CORS preflight blocked error, and the client receives a 502 Bad Gateway error. The developer inspects the Amazon CloudWatch logs for the Lambda function and confirms that the function executed successfully and returned the following raw dictionary:

{
"message": "Shipment created successfully",
"shipmentId": "12345"
}

Which two actions should the developer take to resolve these errors?

  1. Configure CORS on the API Gateway resource to create an OPTIONS method that returns the required Access-Control-Allow-Origin and Access-Control-Allow-Methods headers for the preflight request.Cevap
  2. Modify the Lambda function response to return a formatted JSON object containing statusCode, headers with Access-Control-Allow-Origin, and a stringified body.Cevap
  3. C
    Update the CORS configuration on the Amazon S3 bucket hosting the web application to allow requests from the API Gateway endpoint.
  4. D
    Modify the integration type to Lambda Custom Integration and configure a body mapping template to wrap the raw response.
  5. E
    Increase the execution timeout of the Lambda function and enable active tracing with AWS X-Ray to debug the response serialization.

Cevap

Configure CORS on the API Gateway resource to create an OPTIONS method for the preflight request, and modify the Lambda function response to return a formatted JSON object containing statusCode, headers with Access-Control-Allow-Origin, and a stringified body.
To resolve the CORS preflight blocked and 502 Bad Gateway errors, the developer must address two things. First, the OPTIONS preflight request must be handled by enabling CORS on the API Gateway resource, which configures a Mock integration to return the correct headers. Second, because the API uses a Lambda Proxy Integration, the backend Lambda function must return a properly structured JSON object that includes the statusCode, headers (including Access-Control-Allow-Origin), and a stringified JSON body. The current raw dictionary response format is not recognized by the proxy integration, resulting in a 502 Bad Gateway error.

Adım Adım Çözüm

1
Enable CORS on the target API Gateway resource in the AWS Console or via Infrastructure as Code.
An OPTIONS method is created on the resource with a Mock Integration that returns the Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers headers for browser preflight checks.
This satisfies the browser's initial CORS preflight request.
2
Modify the backend Lambda function code to wrap the response payload in a standardized integration response schema.
The function now returns a JSON structure containing 'statusCode', 'headers' (with 'Access-Control-Allow-Origin' value matching the origin), and 'body' (as a stringified JSON representation of the payload).
This satisfies the response contract required by API Gateway Lambda Proxy integrations and prevents the 502 Bad Gateway error on the actual request.
3
Deploy the API Gateway stage to apply the resource and method modifications.
The changes become active, allowing the client application to successfully complete both the preflight and the actual POST requests.
API Gateway updates do not take effect until the API is deployed to a stage.

Anahtar Kavram

CORS and Lambda Proxy Integration response contracts in Amazon API Gateway.
Tahmini Süre:1m 30s
Bu soruyu puanla