Soru

Zorluk: OrtaAPI Development and Integration with Amazon API Gateway

A developer is configuring a GET method on a REST API in Amazon API Gateway using a Lambda custom integration (non-proxy integration). When the backend AWS Lambda function encounters an invalid input parameter, it returns an error with the message `InvalidParameter`. The client currently receives an HTTP 200200 OK response containing the error message. The developer wants the API Gateway endpoint to return an HTTP 400400 Bad Request status code when this error occurs.

Which configuration steps should the developer take in API Gateway to map this error?

  1. A
    Configure a mapping template in the Integration Request settings to match the string `InvalidParameter` and override the response status code to 400400.
  2. B
    Enable Lambda Proxy Integration in the Integration Request, then configure an HTTP 400400 status code in the Method Response and map the error message in the Integration Response.
  3. Define an HTTP 400400 response in the Method Response settings. Under the Integration Response settings, create a response with the Lambda Error Regex set to `.*InvalidParameter.*` and associate it with the HTTP 400400 Method Response.Cevap
  4. D
    Configure a custom Lambda authorizer for the GET method to inspect the backend response and return a policy that generates an HTTP 400400 Gateway Response.

Cevap

Define an HTTP 400 response in the Method Response settings, and under the Integration Response settings, create a response with the Lambda Error Regex set to match the error string and associate it with the HTTP 400 Method Response.
In a Lambda custom (non-proxy) integration, mapping a backend Lambda error to a specific HTTP status code requires two steps: first, declaring the target HTTP status code (such as 400400) in the Method Response configuration, and second, setting up an Integration Response with a regular expression (Lambda Error Regex) that matches the error message string returned by Lambda (e.g., `.*InvalidParameter.*`) and linking it to the declared Method Response.

Adım Adım Çözüm

1
Add an HTTP 400400 response code to the Method Response settings of the API Gateway method configuration.
This registers the HTTP 400400 status code as a valid output that API Gateway can return to the client.
API Gateway requires all status codes returned by a custom integration to be defined in the Method Response first.
2
Add an Integration Response and configure the Lambda Error Regex to match the pattern `.*InvalidParameter.*`.
This enables API Gateway to parse the `errorMessage` field returned in the Lambda function's error payload and match it using the regular expression.
The Lambda Error Regex matches the error message string returned by AWS Lambda in custom integrations.
3
Set the Method Response Status of the newly created Integration Response to 400400.
Requests triggering the regex match will now return an HTTP 400400 status code to the client instead of the default HTTP 200200.
This maps the matched backend error condition to the registered client-side HTTP status code.

Anahtar Kavram

Lambda custom (non-proxy) integrations require configuring Method Responses and Integration Responses with Lambda Error Regex to map backend errors to client HTTP status codes.
Tahmini Süre:1m 30s
Bu soruyu puanla