Soru

Zorluk: OrtaTroubleshooting API Gateway Errors and CORS

A web-based partner portal hosted on `https://partner.datasync.io` receives a `502 Bad Gateway` error and a CORS block message in the browser console when sending a `PATCH` request to an Amazon API Gateway REST API. The API is configured with a Lambda Proxy integration. The developer checks the Amazon CloudWatch logs and confirms that the backend Lambda function executed successfully and returned the following JSON structure:

{
"statusCode": 200,
"body": "{\"message\": \"Update successful\"}"
}

Which action should the developer take to resolve this error?

  1. Modify the Lambda function response to include the Access-Control-Allow-Origin header in a headers object within the returned JSON.Cevap
  2. B
    Configure a CORS rule in the Amazon S3 bucket hosting the partner portal to allow the domain of the Amazon API Gateway endpoint.
  3. C
    Enable CORS in the Amazon API Gateway console for the PATCH resource, which automatically injects the Access-Control-Allow-Origin header into the Lambda function's payload.
  4. D
    Modify the integration response in the Amazon API Gateway console to map the Access-Control-Allow-Origin header to the client domain.

Cevap

Modify the Lambda function response to include the Access-Control-Allow-Origin header in a headers object within the returned JSON.
In a Lambda Proxy integration, API Gateway expects the backend Lambda function to return a structured JSON response that includes status code, headers, and body. Because the integration bypasses API Gateway's integration response mappings, the Lambda function is solely responsible for returning the `Access-Control-Allow-Origin` header in its response. Without this header, the browser blocks the response, leading to a CORS policy violation and a client-side error.

Adım Adım Çözüm

1
Analyze the error context and integration type.
The endpoint uses a Lambda Proxy integration, meaning API Gateway expects the backend Lambda function to format its output exactly as a JSON object containing statusCode, body, and optionally headers.
Determining the integration type dictates whether API Gateway mapping templates (custom integration) or the Lambda function code (proxy integration) must supply the CORS headers.
2
Inspect the backend Lambda function output.
The function returns statusCode and body but is missing the headers object with Access-Control-Allow-Origin.
For proxy integrations, the browser's CORS requirements are satisfied only if the backend code explicitly provides the Access-Control headers in the returned payload.
3
Update the returned JSON object in the Lambda code.
The function now returns: { "statusCode": 200, "headers": { "Access-Control-Allow-Origin": "https://partner.datasync.io" }, "body": "..." }.
This payload format complies with both the API Gateway proxy integration contract and the browser's CORS policy, resolving the error.

Anahtar Kavram

Handling CORS and response formatting in API Gateway Lambda Proxy integrations.
Bu soruyu puanla