8
votes

I'm new to AWS and I've just successfully setup a Lambda function with RDS connection. Now I'd like to access my new function from outside through the API gateway and passing a few arguments like: "color" : "red"

https://my-api-id.execute-api.region-id.amazonaws.com/flowers?color=red

I've setup everything following the developer guide but unfortunately I'm not able to access the GET parameter in my Python Lambda function.

What I've done so far in my AWS API Gateway:

  • Creating a resource "/flowers" and a GET method
  • GET -> Method Request -> URL Query String Parameters -> Added "color"
  • GET -> Integration Request -> Type: Lambda function
  • GET -> Integration Request -> URL Query String Parameters -> Added name: color, mapped: method.request.querystring.color

I tried to access the color parameter in the lambda handler but the event is always empty and I don't know where the parameter are supposed to be otherwise

def handler(event, context):

    return event     // {}

I think I'm not able to use the body mapping tamplates unless I do not have a request body using GET.

Does anybody know what I need to do in my Python Lambda function, in order to access my color parameter?

1
Thanks for your response! Correct me if I'm wrong but unless I've a GET request, I do not have any request body or even a content-type. I see that I can access the parameters in the the Body Mapping Template using $input.params('querystringkey') but can you explain how I can access this data in my actual lambda function?user3191334
The best and standard way of accessing query string is using integration request only. Even AWS official documentation suggests that way only. Please have a look on 'Create a GET Method with Query Parameters to Call the Lambda Function' section in docs.aws.amazon.com/apigateway/latest/developerguide/… .Alternatively you can use other npm packages like 'querystring' (nodejs.org/api/querystring.html) to get query string, which is not official.Vijayanath Viswanathan

1 Answers

15
votes

Use Lambda Proxy as your integration request type.

And in your handler,

def handler(event, context):

    return {
        'statusCode': 200,
        'body': json.dumps(event),
    }

Your query parameters should be accessible as event['queryStringParameters'].

Reference: Set up a Proxy Resource with the Lambda Proxy Integration