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?
- 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
- BConfigure 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.
- CConfigure 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.
- DConfigure 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
Anahtar Kavram
API Gateway Request Validation