Pass Api Gateway Query Parameters to AWS Lambda

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
4 min

banner

# Table of Contents

  1. Pass Api Gateway Query Parameters to Lambda
  2. Pass Api Gateway Query Parameters to Lambda Proxy
  3. Pass Api Gateway Query Parameters to Lambda Non-Proxy

# Pass Api Gateway Query Parameters to Lambda

There are two types of API Gateway integration for Lambda:

  1. Proxy integration - recommended approach, simply proxy request and response data, including query string parameters.
  2. Non-proxy integration - outdated approach, adds to the latency of the API but gives you more control. You are responsible for mapping query string parameters.

# Pass Api Gateway Query Parameters to Lambda Proxy

To pass API Gateway Query parameters to a Lambda function with proxy integration:

  1. Open the AWS API Gateway console and click on your API's name.
  2. In the Resources tab, click on the HTTP method for a specific route and select Integration Request.
  3. Check the Use Lambda Proxy integration checkbox and set the Integration Type to Lambda Function, selecting your function.

integration request proxy

  1. Save the changes, click on Actions and select Deploy API.

deploy proxy integration

The response of a Lambda proxy integration has the following properties:

lambda-proxy-response
{ "statusCode": 200, "headers": { "Content-Type": "application/json"}, "body": "..." }

Where the body must be JSON stringified (passed to JSON.stringify() in Node or json.dumps() in Python).

You can access the query string parameters on the event object:

  • In Python - event['queryStringParameters']['myQueryParam'].
  • In Node.js - event.queryStringParameters.myQueryParam.

Here's a complete example of a Node.js Lambda function with proxy integration that takes in 2 query parameters - page and limit.

The function simply returns the query parameters to the caller.

lambda-proxy
exports.handler = async event => { const response = { statusCode: 200, body: JSON.stringify({ // 👇️ access query parameters on event object page: event.queryStringParameters.page, limit: event.queryStringParameters.limit, }), }; return response; };

In the screenshot below I invoke the function with 2 query parameters - page and limit:

invoke proxy lambda with query params

# Pass API Gateway Query Parameters to Lambda Non-Proxy

When configuring an API Gateway with lambda non-proxy integration, we are responsible for mapping query string parameters.

To pass Api Gateway query string parameters to a Lambda function, using non-proxy integration, you have to:

  1. Open the AWS API Gateway console and click on your API's name.
  2. In the Resources tab, click on the specific HTTP method.
  3. Click on Method Request and expand the URL Query String Parameters section.
  4. Click on Add query string and type in the name of your query string parameter. You can add multiple query string parameters and you can optionally make them Required.
  5. Once you've added your query string parameters click on the check mark to the right. In the screenshot below I have added 2 query parameters - page and limit.

non-proxy method request

  1. Click on Method Execution at the top of the screen to go back and select Integration Request.
  2. Expand the Mapping Templates section and having selected When no template matches the request Content-Type header click on Add mapping template, type in application/json and click on the check mark to save.

integration request content type

  1. On the popup screen select Yes, secure this integration.

secure integration

  1. The radio button of Request passthrough will be switched to When there are no templates defined (recommended).

  2. In the editor below, add the following mapping template. Make sure to replace page and limit with the names of your query parameters:

mapping-template
#set($inputRoot = $input.path('$')) { "page": "$input.params('page')", "limit": "$input.params('limit')" }

non proxy mapping template

  1. Click on the save button at the bottom.

  2. Scroll back to the top and click on Actions and select Deploy API.

deploy non proxy integration

Your lambda function can access the query string parameters directly on the event object:

  • In Python - event['myQueryParam']
  • In Node.js - event.myQueryParam

Here is an example Node.js function that accesses and returns the page and limit query parameters:

lambda-non-proxy
exports.handler = async event => { console.log(JSON.stringify(event, null, 2)) const response = { // 👇️ access query parameters page: event.page, limit: event.limit, }; return response; };

In the screenshot below I invoke the function with 2 query parameters - page and limit:

lambda non proxy response

I've also written tutorials on how to provision and configure API Gateway and HTTP API in AWS CDK:

# Further Reading

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.