Soru

Zorluk: OrtaAPI Development and Integration with Amazon API Gateway

A developer is optimizing a serverless application where an Amazon API Gateway REST API is integrated with an AWS Lambda function. Currently, the Lambda function performs validation on the incoming JSON request body to verify the existence of mandatory fields. If the validation fails, the Lambda function returns a custom error response. To reduce Lambda invocation costs and latency, the developer wants to reject invalid requests before they reach the backend. Which solution should the developer implement to meet these requirements?

  1. Configure request validation in API Gateway by creating a model that defines the required fields using JSON Schema, and enable request body validation on the method.Cevap
  2. B
    Configure the API Gateway method to use a Lambda proxy integration, which automatically parses and validates the structure of the incoming JSON payload before invoking the backend function.
  3. C
    Configure a custom Lambda authorizer for the API Gateway method to inspect the request body and reject requests with missing fields before the main backend Lambda function is invoked.
  4. D
    Configure Cross-Origin Resource Sharing (CORS) headers in API Gateway and set the Access-Control-Allow-Headers to restrict access to requests containing only the required fields.

Cevap

Configure request validation in API Gateway by creating a model that defines the required fields using JSON Schema, and enable request body validation on the method.
Configuring a Request Validator with a JSON Schema model directly on the API Gateway method allows API Gateway to perform the verification at the edge. If the payload does not contain the mandatory fields, API Gateway immediately blocks the request and returns a 400 Bad Request error. This prevents the request from triggering the integration, meaning the backend Lambda function is never executed, thereby saving invocation costs and reducing response latency for malformed requests.

Adım Adım Çözüm

1
Define a JSON Schema model representing the required payload structure in API Gateway.
API Gateway has a representation of the expected fields and validation rules.
This sets up the rules needed to validate the incoming JSON object properties.
2
Associate the JSON Schema model with the method request and enable the request validator for the body.
API Gateway is configured to intercept incoming requests and validate them against the model.
This allows API Gateway to automatically reject requests that do not match the schema with a 400 Bad Request status code, preventing the backend Lambda function from being invoked.

Anahtar Kavram

API Gateway Request Validation
Bu soruyu puanla