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?
- 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.Cevap
- BEnable 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.
- CCreate 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.
- DDefine 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.
Cevap
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.
Adım Adım Çözüm
Anahtar Kavram
Error mapping in API Gateway Lambda Custom (Non-Proxy) Integrations