Soru

Zorluk: OrtaTroubleshooting API Gateway Errors and CORS

A mobile application sends a `POST` request to an Amazon API Gateway REST API. The API is configured with a Lambda proxy integration to retrieve user profiles. The developer recently migrated the integration from a Lambda custom integration to a Lambda proxy integration. Following the migration, client requests fail with a `502 Bad Gateway` status code and a CORS error in the browser console. The Lambda function execution logs show that the function completes successfully and returns the user profile data. Which two actions should the developer take to resolve this issue?

  1. Modify the backend Lambda function to return a JSON object containing `statusCode`, a headers map, and a stringified JSON `body`.Cevap
  2. Include the `Access-Control-Allow-Origin` header inside the `headers` object of the Lambda function's return payload.Cevap
  3. C
    Configure an integration response mapping template in API Gateway to map the Lambda output to the client-facing format.
  4. D
    Enable CORS on the API Gateway resource and rely on API Gateway to automatically inject CORS headers into the integration response.
  5. E
    Configure a CORS origin policy on the Amazon S3 bucket that hosts the static assets of the mobile application.

Cevap

To resolve the 502 Bad Gateway and CORS errors, the developer must format the Lambda function's output as a JSON object containing statusCode, a headers map, and a stringified body, and explicitly include the Access-Control-Allow-Origin header inside the headers map.
Under a Lambda proxy integration, API Gateway expects the backend Lambda function to structure its response as a JSON object with the keys: `statusCode`, `headers`, and `body` (as a string). Returning unformatted output causes API Gateway to fail parsing the integration response, throwing a 502 Bad Gateway error. Because the request never successfully completes with valid response headers, the browser console also reports a CORS error. Furthermore, API Gateway does not inject CORS headers automatically into proxy integration responses, meaning the Lambda function itself must return the `Access-Control-Allow-Origin` header in its response payload.

Adım Adım Çözüm

1
Inspect the Lambda function response format.
The Lambda function returns raw user profile data, which is incompatible with Lambda proxy integration requirements.
Lambda proxy integrations require a structured response containing statusCode, headers, and body.
2
Modify the response structure returned by the Lambda function.
The function returns a JSON object with 'statusCode': 200, a 'headers' map, and 'body': JSON.stringify(profileData).
This matches the expected schema for API Gateway to successfully parse the integration response and avoid the 502 Bad Gateway error.
3
Add the Access-Control-Allow-Origin header to the Lambda response.
The headers map contains 'Access-Control-Allow-Origin': '*'.
Since proxy integrations bypass API Gateway integration response formatting, the backend Lambda must explicitly supply the CORS headers to prevent browser blocks.

Anahtar Kavram

Lambda Proxy Integration Response Format and CORS Requirements
Bu soruyu puanla