Question

Difficulty: MediumTroubleshooting API Gateway Errors and CORS

A React Native mobile application sends an HTTP POST request to an Amazon API Gateway REST API endpoint to update a user's profile. The API uses a Lambda proxy integration with an AWS Lambda function. Users report receiving a 502 Bad Gateway error when saving their profile updates. The developer checks the CloudWatch logs for the Lambda function and confirms it executes successfully, returning the following output:

{
"message": "Profile updated successfully",
"status": "success"
}

Which of the following explains why the application receives the 502 Bad Gateway error and identifies the correct solution?

  1. The Lambda function response format is incorrect for a Lambda proxy integration. The developer must modify the Lambda function to return a JSON object containing an integer 'statusCode' and a stringified 'body'.Answer
  2. B
    The API Gateway integration is configured to expect a custom integration response mapping. The developer must configure an Integration Response mapping template in API Gateway to map the Lambda output keys to JSON.
  3. C
    The API Gateway resource is missing a CORS configuration for the POST method. The developer must enable CORS on the API Gateway resource and redeploy the API.
  4. D
    The API Gateway execution role lacks the permissions required to invoke the Lambda function. The developer must update the Lambda function's resource-based policy to grant 'lambda:InvokeFunction' permission to API Gateway.

Answer

The Lambda function response format is incorrect for a Lambda proxy integration. The developer must modify the Lambda function to return a JSON object containing an integer 'statusCode' and a stringified 'body'.
The correct response points out that in a Lambda proxy integration, API Gateway expects the backend Lambda function to return a structured JSON object containing a 'statusCode' (number) and a 'body' (which must be a stringified representation of the payload). Returning a custom JSON object directly without this structure results in a 502 Bad Gateway error and a 'Malformed Lambda proxy response' entry in the API Gateway logs.

Step-by-Step Solution

1
Analyze the error message and the configuration context.
The client receives a 502 Bad Gateway error, but the Lambda function executes successfully and returns a custom JSON object.
This indicates that the integration between API Gateway and Lambda is working, but API Gateway is unable to parse the returned output.
2
Evaluate the response requirements for the configured integration type.
Since the API uses a Lambda proxy integration, the backend Lambda function is responsible for defining the entire HTTP response, including status codes, headers, and the body.
For Lambda proxy integrations, API Gateway expects a specific structure: { 'statusCode': number, 'body': 'string', 'headers': { ... } }.
3
Identify the formatting issue in the Lambda function's current output.
The current output is a plain JSON object with custom keys ('message', 'status'), which causes API Gateway to fail with a malformed proxy response error.
Changing the function code to return the correct envelope with a stringified body resolves the malformed response and allows API Gateway to map it to a proper HTTP response.

Key Concept

API Gateway Lambda Proxy Integration Response Requirements
Rate this question