A developer is building a serverless backend using Amazon API Gateway and AWS Lambda. The API Gateway REST API is configured with a Lambda custom (non-proxy) integration. When the client sends invalid input, the Lambda function throws an exception with the error message: `[ValidationFailed] User age is under the legal limit`. The developer wants API Gateway to return a `400 Bad Request` HTTP response with a custom JSON payload when this error occurs. Which configuration steps must the developer perform in API Gateway to achieve this?
- Configure a Method Response for the 400 status code. In the Integration Response, configure an entry with a Lambda Error Regex pattern set to `.*ValidationFailed.*`, map it to the 400 Method Response, and define a body mapping template to customize the returned JSON payload.Answer
- BEnable Lambda proxy integration, and configure an Integration Response with a Lambda Error Regex pattern set to `.*ValidationFailed.*` to map the exception to a 400 Method Response.
- CConfigure a custom Lambda authorizer on the method to validate the input payload, and set the Gateway Response for 400 bad requests to transform the exception message.
- DDefine the regex pattern `.*ValidationFailed.*` directly in the Method Response configuration, and map the matching pattern to the integration request body mapping template to mock the response without executing the Lambda function.
Answer
Configure a Method Response for the 400 status code, create an Integration Response with a Lambda Error Regex matching the error message (such as `.*ValidationFailed.*`), link it to the 400 Method Response, and define a body mapping template.
The correct answer correctly outlines the requirements for mapping Lambda errors to custom HTTP responses in a non-proxy (custom) integration. This involves: 1) Declaring the HTTP status code (400) in the Method Response. 2) Creating an Integration Response with a selection pattern regex (such as `.*ValidationFailed.*`) that matches the Lambda function's error message. 3) Specifying a VTL mapping template to shape the output payload returned to the client.
Step-by-Step Solution
Key Concept
API Gateway integration and response mapping patterns for Lambda non-proxy integrations