Question

Difficulty: HardTroubleshooting API Gateway Errors and CORS

A developer is migrating a backend REST API from a Lambda Custom Integration to a Lambda Proxy Integration in Amazon API Gateway. The client application is an iOS mobile app that sends a POST request to create user profiles. Previously, when the Lambda function encountered a validation error (such as a missing email address), it would throw an exception, and the developer mapped this exception to a 400 Bad Request HTTP status code using API Gateway Integration Responses. After switching the API method to use the Lambda Proxy Integration, the client application receives a 502 Bad Gateway error instead of the 400 Bad Request validation error, even though the Lambda function execution succeeds with the expected validation error logged. Which of the following modifications should the developer make to the Lambda function's code to resolve this issue and return the expected 400 Bad Request status code?

  1. A
    Configure an Integration Response in the API Gateway method execution console with a regular expression mapping to capture the thrown exception and map it to a 400 HTTP status code.
  2. Modify the Lambda function to catch the validation error and return a JSON object containing a statusCode key set to 400 and a body key containing a serialized JSON string of the error details.Answer
  3. C
    Add an Access-Control-Allow-Origin header to the API Gateway OPTIONS method configuration and configure the static web host's S3 bucket policy to allow external CORS requests.
  4. D
    Implement a custom Lambda Authorizer to validate the request payload and return an IAM policy with a context block containing the statusCode mapping of 400.

Answer

Modify the Lambda function to catch the validation error and return a JSON object containing a statusCode key set to 400 and a body key containing a serialized JSON string of the error details.
In a Lambda Proxy Integration, Amazon API Gateway expects the backend Lambda function to return a response in a specific JSON format containing statusCode (as an integer) and body (as a stringified JSON). If the Lambda function throws an unhandled exception or returns a structure that does not conform to this contract, API Gateway cannot parse the output and returns a 502 Bad Gateway error to the client. To properly return a client-side error like 400 Bad Request in a proxy integration, the function must catch the exception and return the correct JSON format directly.

Step-by-Step Solution

1
Analyze the API Gateway integration type and the error returned.
The integration is Lambda Proxy Integration, and the client receives a 502 Bad Gateway error instead of the expected 400 Bad Request.
502 Bad Gateway errors in Lambda Proxy Integrations typically occur when the Lambda function's output does not conform to the expected format required by API Gateway.
2
Review the differences in error handling between Lambda Custom and Lambda Proxy integrations.
In Custom Integrations, API Gateway maps errors using Integration Responses. In Proxy Integrations, API Gateway relies entirely on the Lambda function returning a structured JSON response containing the status code and body.
To resolve the 502 error and return a 400 status code, the responsibility of mapping the error shifts from API Gateway configuration to the Lambda function code.
3
Formulate the correct Lambda function response payload.
The Lambda function must catch the validation exception and return an object with a statusCode of 400 and a serialized string body detailing the validation failure.
This satisfies the Lambda Proxy Integration response contract, allowing API Gateway to parse the payload and pass the 400 status code to the client.

Key Concept

API Gateway Lambda Proxy Integration Response Formatting Requirements
Rate this question