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']