Question

Difficulty: MediumAPI Development and Integration with Amazon API Gateway

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?

  1. Configure an integration response in API Gateway with a regular expression pattern matching '.*InvalidParameter.*', and map it to a 400 method response.Answer
  2. B
    Modify the Lambda function response to return a JSON object containing a 'statusCode' key set to 400 and a 'body' key containing the error message.
  3. C
    Create a custom Lambda authorizer that validates the parameter values, and returns a 400 HTTP response code if validation fails.
  4. D
    Enable Cross-Origin Resource Sharing (CORS) on the resource and add the Access-Control-Allow-Origin header to the method response.

Answer

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.

Step-by-Step Solution

1
Define the Method Response in API Gateway.
Add an HTTP 400 status code to the Method Response settings for the resource's method.
Before API Gateway can return a 400 status code to the client, the status code must be declared in the Method Response configuration.
2
Configure the Integration Response mapping in API Gateway.
Create a new Integration Response, set the Lambda Error Regex to match the error pattern (e.g., '.*InvalidParameter.*'), and map it to the 400 Method Response.
This tells API Gateway to inspect the error message returned by Lambda and, if it matches the pattern, return the 400 status code instead of the default 200 OK.
3
Deploy the API to a stage.
The configuration changes are pushed live.
Changes to API Gateway configurations only take effect after deploying the API to a stage.

Key Concept

API Gateway Custom Integration Error Handling
Estimated Time:1m 30s
Rate this question