A developer is configuring a REST API using Amazon API Gateway with a backend AWS Lambda function using custom (non-proxy) integration. The Lambda function throws an error containing the string 'InvalidParameter' when the input is malformed, but the client receives an HTTP 200 OK status response with the error message in the payload. What configuration change is required in API Gateway to ensure the client receives an HTTP 400 Bad Request status code?
- Configure an integration response in API Gateway with a regular expression pattern matching '.*InvalidParameter.*', and map it to a 400 method response.Cevap
- BModify the Lambda function response to return a JSON object containing a 'statusCode' key set to 400 and a 'body' key containing the error message.
- CCreate a custom Lambda authorizer that validates the parameter values, and returns a 400 HTTP response code if validation fails.
- DEnable Cross-Origin Resource Sharing (CORS) on the resource and add the Access-Control-Allow-Origin header to the method response.
Cevap
Configure an integration response in API Gateway with a regular expression pattern matching '.*InvalidParameter.*', and map it to a 400 method response.
In custom (non-proxy) integrations with API Gateway, the backend response is processed through integration responses. If the backend function throws an error, API Gateway maps it to an HTTP status code using regular expressions matched against the 'errorMessage' field in the Lambda response. Therefore, configuring an integration response with a regex matching the error pattern and mapping it to a 400 method response is the correct approach.
Adım Adım Çözüm
Anahtar Kavram
API Gateway Custom Integration Error Handling
Tahmini Süre:1m 30s