Question

Difficulty: HardAPI Development and Integration with Amazon API Gateway

A developer is maintaining a legacy REST API in Amazon API Gateway that integrates with an AWS Lambda function using a Lambda custom (non-proxy) integration. When client input fails validation, the Lambda function throws an exception returning the string `[ValidationFailed] Input values must be alphanumeric`. Currently, the client receives a 200 OK response with the error message in the payload. The developer wants the API to return an HTTP 400 Bad Request status code and a JSON payload containing only the error message when validation fails.

Which configuration steps should the developer perform to return the correct error response to the client?

  1. Define a 400 Method Response for the API method. In the Integration Response configuration, add a new response with the Lambda Error Regex set to `.*ValidationFailed.*`, map it to the 400 Method Response, and define a body mapping template to extract and format the error message.Answer
  2. B
    Enable Lambda Proxy Integration for the API method. Modify the Lambda function to throw the `[ValidationFailed]` exception so that API Gateway automatically maps the exception type to an HTTP 400 Bad Request status code.
  3. C
    Create a custom Lambda Authorizer for the API. Configure the authorizer to inspect the incoming request parameters, throw a validation exception if they are not alphanumeric, and map the authorizer output to an HTTP 400 response.
  4. D
    Define a 400 Method Response. In the Integration Request configuration, create a mapping template for the Content-Type `application/json` that maps the Lambda error string `.*ValidationFailed.*` to an HTTP 400 status code.

Answer

Define a 400 Method Response for the API method. In the Integration Response configuration, add a new response with the Lambda Error Regex set to `.*ValidationFailed.*`, map it to the 400 Method Response, and define a body mapping template to extract and format the error message.
To map custom errors in a Lambda custom (non-proxy) integration, you must declare the target HTTP status code in the Method Response. Then, in the Integration Response, you define a regular expression matching the error pattern thrown by the Lambda function (such as `.*ValidationFailed.*`), select the corresponding Method Response status code, and configure a mapping template to format the output payload.

Step-by-Step Solution

1
Configure the Method Response in API Gateway to include the HTTP 400 status code.
The API method is prepared to return a 400 status code to client applications.
Method Responses define the valid HTTP status codes that API Gateway can send back to the client.
2
Configure the Integration Response in API Gateway by creating a new entry with the Lambda Error Regex set to `.*ValidationFailed.*`.
API Gateway will match errors containing the validation failure pattern returned from the Lambda function.
In non-proxy integrations, API Gateway checks the `errorMessage` field from the Lambda response against regular expressions to determine which Method Response to trigger.
3
Map the matching integration response to the 400 Method Response and add a response mapping template.
The validation error response is mapped to HTTP status 400 and its body is structured as JSON.
The mapping template extracts the error message and formats it into the clean JSON format required by the client.

Key Concept

Error mapping in API Gateway Lambda Custom (Non-Proxy) Integrations
Rate this question