Soru

Zorluk: OrtaAPI Development and Integration with Amazon API Gateway

A developer is building a serverless application where Amazon API Gateway routes requests to a backend AWS Lambda function using a Lambda custom (non-proxy) integration. The Lambda function expects a JSON input containing a key named `userId`. However, client requests send this identifier as a query string parameter named `uid` (for example, `/users?uid=123`). How should the developer configure API Gateway to map the incoming query string parameter to the JSON payload format required by the Lambda function?

  1. In the Integration Request settings, add a mapping template for the application/json content type, and define the template body as {"userId": "$input.params('uid')"}.Cevap
  2. B
    In the Integration Request settings, enable Lambda Proxy Integration to automatically convert the uid query string parameter into a root-level key named userId in the event payload.
  3. C
    Configure a custom Lambda Authorizer for the Method Request to extract the uid query string parameter and rewrite it into the request body before the integration takes place.
  4. D
    Enable Lambda Proxy Integration and configure a Method Response mapping template to transform the request payload format before it is forwarded to the backend Lambda function.

Cevap

In the Integration Request settings, add a mapping template for the application/json content type, and define the template body as {"userId": "$input.params('uid')"}.
In a Lambda custom (non-proxy) integration, request mapping templates are used to shape the payload before it is dispatched to the backend. Using Velocity Template Language (VTL), the developer can define a template for the application/json content type. The expression $input.params('uid') extracts the value of the uid query string parameter and maps it to the userId key in the constructed JSON payload.

Adım Adım Çözüm

1
Identify the integration type and requirements
The API uses a Lambda custom (non-proxy) integration and needs to transform a query parameter into a custom JSON payload structure.
Custom integrations require developers to explicitly define the mapping of request components using mapping templates.
2
Select the correct location for request transformation
The transformation must happen during the Integration Request phase before the request is sent to the backend.
This is where API Gateway processes mapping templates for outgoing backend payloads.
3
Construct the mapping template using Velocity Template Language (VTL)
Create an application/json template containing the structure {"userId": "$input.params('uid')"}.
The $input.params() function retrieves the value of parameters from headers, path variables, or query strings.

Anahtar Kavram

Request transformation using API Gateway mapping templates for Lambda custom integrations.
Bu soruyu puanla