Soru

Zorluk: ZorAPI Development and Integration with Amazon API Gateway

An organization has a REST API exposed via Amazon API Gateway that routes client requests to a backend AWS Lambda function. To reduce Lambda invocation costs, the developer needs to validate that incoming JSON request payloads contain a required `accountId` string field before the requests are forwarded to the backend. If a request is invalid, API Gateway must return an HTTP 400 Bad Request response directly to the client without invoking the backend function.

Which configuration steps should the developer perform to meet these requirements?

  1. Create a Model using JSON Schema to define the required `accountId` property, associate it with the method request body, and set the Method Request validator to validate the request body.Cevap
  2. B
    Configure a Lambda Proxy integration and use a Velocity Template Language (VTL) mapping template in the Integration Request to inspect the payload and reject invalid requests.
  3. C
    Configure a Lambda Authorizer to parse the incoming request body, validate the schema structure, and return an Allow policy only if the `accountId` field is present.
  4. D
    Set up a custom Gateway Response mapping template for the default Integration Response that validates the incoming payload before executing the integration.

Cevap

Create a Model using JSON Schema to define the required `accountId` property, associate it with the method request body, and set the Method Request validator to validate the request body.
The correct option is to define a Model using JSON Schema, associate it with the request body in the Method Request settings, and enable the Request Validator to validate the request body. When request validation is enabled, API Gateway validates the request payload before forwarding it to the integration backend. If the validation fails, API Gateway immediately returns a 400 Bad Request response without invoking the integration (the backend Lambda function), minimizing Lambda executions and costs.

Adım Adım Çözüm

1
Define a Model in API Gateway using a JSON Schema draft-4 structure that specifies `accountId` as a required property.
The API Gateway has a template to validate incoming payload structures against.
This establishes the validation contract for incoming payloads.
2
Go to the Method Request settings for the resource's POST/PUT method, configure the Request Body content type (e.g., application/json), and assign the created Model.
The method is now configured to expect payload structures adhering to the defined schema.
This links the model definition to the specific HTTP method request phase.
3
Set the Method Request's 'Request Validator' configuration to 'Validate body'.
API Gateway automatically validates the body against the Model before routing the request to the integration.
This ensures API Gateway blocks invalid requests at the method request layer, returning a 400 Bad Request without invoking the Lambda backend.

Anahtar Kavram

API Gateway Request Validation with Models
Tahmini Süre:2m 0s
Bu soruyu puanla