34

Say I want to pass val1 and val2 in the URL string when making a GET request from my Api gateway endpoint to my Lambda function:

https://xyz.execute-api.amazonaws.com/prod/test?val1=5&val2=10

And I have a simple function that sums the two inputs, val1 and val2:

def lambda_handler(event, context):
    # How do I get at val1 and val2??
    return {'result': val1 + val2}

I've added val1 and val2 to URL Query String Parameters on the Method Request on the AWS API Gateway. But how do I access them inside the function?

Mohamed Salem Lamiri
  • 5,767
  • 6
  • 34
  • 47
capitalistcuttle
  • 1,709
  • 2
  • 20
  • 28
  • Have you tried to print both `event` and `context`? You might take a look at the [sample in the docs](http://docs.aws.amazon.com/lambda/latest/dg/python-programming-model-handler-types.html) – albert Dec 20 '15 at 23:11
  • 1
    If you pass your data correctly, you should be able to access the `event` object with something like `event.val1` (at least that's how you access an object's properties in JS) – JohnAllen Dec 21 '15 at 02:49
  • Possible duplicate of [How to pass a querystring or route parameter to AWS Lambda from Amazon API Gateway](https://stackoverflow.com/questions/31329958/how-to-pass-a-querystring-or-route-parameter-to-aws-lambda-from-amazon-api-gatew) – MEE Sep 19 '18 at 14:16

2 Answers2

29

After defining the query string parameters in Method Request section of the API Gateway, you then need to define a Mapping Template in the Method Execution section.

In the Method Execution section, select Mapping Templates and then click Add Mapping Template. Enter application/json for the Content Type and then create a mapping template that looks something like this:

{
    "va1": "$input.params('val1')",
    "val2": "$input.params('val2')"
}

This will tell API Gateway to take the input parameters (either passed on the path, or in headers, or in query parameters) called val1 and val2 and send them to the Lambda function in the event data as val1 and val2.

garnaat
  • 44,310
  • 7
  • 123
  • 103
  • 4
    Thanks. I'd add that the Mapping Templates are under Integration Request on the Method Execution page. This link was helpful: https://forums.aws.amazon.com/thread.jspa?threadID=192601. Still having a few problems, but at least I know where to look. – capitalistcuttle Dec 22 '15 at 20:09
  • 3
    Maybe I'll find some reason for all these layers, but have to say that at first glance this is MUCH more fiddly than I'd imagined. Python is so introspectable that AWS Lambda could just create the mapping automatically by inspecting the function signature. – capitalistcuttle Dec 22 '15 at 20:12
  • 2
    This link cleared up some details about how to include various types of parameters when editing the Mapping Template: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference – pythonjsgeo Mar 09 '16 at 23:23
  • You no longer need to define Mapping templates in order to access your query request params. Another solution consists of using Lambda Proxy integration as described in this answer. https://stackoverflow.com/questions/31329958/how-to-pass-a-querystring-or-route-parameter-to-aws-lambda-from-amazon-api-gatew/46114185#46114185 – Mohamed Salem Lamiri Sep 24 '18 at 10:29
13

All the information can be retrieved from Event object.

For example : The value of a variable foo can be retrieved from the event as : event["foo"].

Batman
  • 301
  • 3
  • 4