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?
- 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'.Cevap
- BThe 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.
- CThe 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.
- DThe 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.
Cevap
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.
Adım Adım Çözüm
Anahtar Kavram
API Gateway Lambda Proxy Integration Response Requirements