Last updated: Feb 26, 2024
Reading time·4 min
There are two types of API Gateway integration for Lambda:
To pass API Gateway Query parameters to a Lambda function with proxy integration:
Resources
tab, click on the HTTP method for a specific route and
select Integration Request
.Use Lambda Proxy integration
checkbox and set the
Integration Type
to Lambda Function
, selecting your function.Actions
and select Deploy API
.The response of a Lambda proxy integration has the following properties:
{ "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:
event['queryStringParameters']['myQueryParam']
.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.
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
:
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:
Resources
tab, click on the specific HTTP method.Method Request
and expand the URL Query String Parameters
section.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
.page
and
limit
.Method Execution
at the top of the screen to go back and select
Integration Request
.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.Yes, secure this integration
.The radio button of Request passthrough
will be switched to
When there are no templates defined (recommended)
.
In the editor below, add the following mapping template. Make sure to
replace page
and limit
with the names of your query parameters:
#set($inputRoot = $input.path('$')) { "page": "$input.params('page')", "limit": "$input.params('limit')" }
Click on the save button at the bottom.
Scroll back to the top and click on Actions
and select Deploy API
.
Your lambda function can access the query string parameters directly on the
event
object:
event['myQueryParam']
event.myQueryParam
Here is an example Node.js function that accesses and returns the page
and
limit
query parameters:
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
:
I've also written tutorials on how to provision and configure API Gateway and HTTP API in AWS CDK: