Soru

Zorluk: ZorAPI Development and Integration with Amazon API Gateway

A developer is configuring an Amazon API Gateway REST API with an HTTP custom (non-proxy) integration that connects to an on-premises backend service. The backend service always returns an HTTP 200 OK status code, even when an application-level error occurs. When such an error occurs, the backend JSON response body contains the field "errorCode": "INVALID_PARAMETERS". The developer needs the API Gateway to return an HTTP 400 Bad Request status code to the client instead of HTTP 200 OK when this error is present.

Which configuration strategy should the developer use to achieve this?

  1. Deploy an intermediate AWS Lambda function using Lambda integration to call the backend service, inspect the response payload, and return the appropriate HTTP status code to API Gateway.Cevap
  2. B
    Create a Velocity Template Language (VTL) mapping template within the 200 OK Integration Response to dynamically override the method response status code to 400 if the payload contains the error code.
  3. C
    Create an Integration Response with an HTTP status regex pattern of .*INVALID_PARAMETERS.* to match the backend response body and map it to the HTTP 400 Method Response.
  4. D
    Configure a Lambda Authorizer in API Gateway to intercept the backend response, inspect the JSON payload, and return a response policy that overrides the HTTP status code to 400.

Cevap

Deploy an intermediate AWS Lambda function using Lambda integration to call the backend service, inspect the response payload, and return the appropriate HTTP status code to API Gateway.
The correct strategy is to deploy an intermediate AWS Lambda function. Because the backend service always returns an HTTP 200 OK status code, API Gateway cannot natively select a different integration response based on the JSON body content. By routing the request through a Lambda function, the function can inspect the legacy backend response body and return a response that allows API Gateway to map it to an HTTP 400 Bad Request client response.

Adım Adım Çözüm

1
Analyze the limitations of native API Gateway HTTP custom integrations regarding response mapping.
Identify that in an HTTP custom integration, API Gateway evaluates the HTTP status code returned by the integration endpoint to select an Integration Response, not the JSON payload fields.
This rules out natively selecting a 400 response from a successful 200 OK HTTP integration based on JSON body parameters.
2
Evaluate the capabilities of VTL mapping templates in custom integrations.
Confirm that mapping templates are executed after the Integration Response selection phase and cannot be used to dynamically change the Method Response HTTP status code based on response body analysis.
This rules out configuring dynamic status overrides within mapping templates in REST APIs.
3
Select a solution that provides custom programming logic to process backend payloads.
Introduce an intermediate AWS Lambda function to intercept the backend response, parse the JSON payload, check for the error string, and structure a custom API Gateway response or raise a Lambda error.
Since API Gateway cannot natively perform content-based routing on HTTP backend responses, an intermediate compute layer like Lambda is required to inspect and map the response.

Anahtar Kavram

API Gateway custom integration response mapping constraints
Bu soruyu puanla