How to Pass the Query String Parameters to AWS Lambda Function or HTTP Endpoint?

03 Feb, 2022
In this article, we will explore the process of passing query string parameters from a client to the Backend Lambda or HTTP endpoint API gateway. As we know there are two types of integrations for Lambda and HTTP endpoints. The first kind is Proxy integration and second kind is Non-proxy integration. While proxy implementation is the recommended approach as it simplifies the management of the API and reduces API response time, non-proxy implementation forces the use of mapping templates.
Although templates are supported with use in API Gateway, they use VTL. As VLT language support is not supported by the AWS support team, clients who want to customize templates for their use case must have knowledge of the VLT language. There are two types of integration:
Proxy Integration:
 When a client submits an API request, API gateway passes the raw request to the backend as-is, except that the order of the request parameters is not preserved. Request parameters can be referred as headers, query strings, path parameters and so on.
Non-proxy Integration:
 With this integration, you must be sure that the input to the back end is supplied as the integration request payload. This implies that you, as an API developer must map the query string parameters, along with any input data the client supplied as a request parameter, into the proper integration request body using a mapping template.
Step 1: Open API Gateway Management Console and choose the API.
Step 2: Choose the resources. Choose the configured HTTP method.
Step 3: Choose the Integration Request.
Step 4: Select HTTP or Lambda as integration type. Check the box for “Use HTTP Proxy Integration” for the HTTP backend or use Lambda proxy integration for the lambda backend. Select the “HTTP method” supported by the backend. Provide an “Endpoint URL”. For example, a pet store endpoint. Select the Lambda function and accept the other default settings. Finally, choose to save.
Step 5: Now deploy the API by clicking on the Actions tab.
For this you must be sure that the input to the lambda function is supplied as the integration request payload. This implies that, you as an API developer must map query string parameters along with any input data the client supplied as request parameters into the proper integration request body using a mapping template. The following are the steps for the same:
Step 1: Open the API Gateway console and choose the API.
Step 2: Choose the resources. Choose the configured HTTP method.
Step 3: In the method execution panel choose Method Request.
Step 4: Expand the URL Query String Parameters section and choose “Add query string”.
Step 5: Enter “foo” for the name, select the required option and choose the check mark icon to save the setting.  Select Caching option as per requirement return to the method execution panel and then choose Integration Request.
Step 6: Expand the mapping template section choose Add Mapping Template. Enter application JSON for content type, choose the check mark icon to save the setting in the pop-up that appears. Choose “yes secure this integration”.
Step 7: Check the recommended “when there are no templates defined “for request body pass through. Replace the generated mapping script in the mapping template editor with the following:
#set($inputRoot = $input.path('$'))
{
"foo": "$input.params('foo')"
}
Here the input dot params returns the value of method request parameter from the path query string or header value. Given a parameter names string “foo”. The input dot params returns the value of a method request parameter from the path, query-string, or header value. Given a parameter name string foo. This given template will assign the value returned by input.params to a variable named “foo” which can be retrieved at the backend.
Step 8: Then choose save and deploy the API.
sumitgumber28
AWS
aws-lambda
Cloud-Computing
Amazon Web Services
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Amazon VPC Networking Components
AWS DynamoDB - Insert Data Using AWS Lambda
Amazon S3 - Storage Classes
Amazon EC2 - Instance Types
Amazon S3 - Cross Region Replication
Amazon S3 - Lifecycle Management
Amazon Web Services - Introduction to Simple Queue Service(SQS)
AWS DynamoDB - Working with Backups
AWS DynamoDB - Working with Streams
AWS DynamoDB - PartiQL Delete Statements For DynamoDB