Access Query Strings, Path Parameters & HTTP Headers in Lambda Functions Behind Amazon API Gateway

It’s common for APIs to have parameters, either as query strings — GET /object?name=book — or as path parameters — GET /object/book. But if your APIs are implemented in Lambda functions, how do you access the values of these parameters in a Lambda function? Here are a few ways:

Using Lambda Proxy Integration

Turn on Lambda proxy integration in API Gateway:

Then access parameters in your Lambda as follows:

event['pathParameters']['myparam']
event["queryStringParameters"]['myparam']
event['requestContext']['identity']['userAgent']

If you use proxy integration, your Lambda must respond in this format:

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "body": "..."
}

Using Mapping Templates

Set the method request passthrough mapping template in API Gateway:

Then access parameters in your Lambda as follows:

event['params']['querystring']['token']
event['params']['querystring']['uid']

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.