14

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?

user3191334
  • 1,148
  • 3
  • 15
  • 33
  • 1
    Possible duplicate of [Aws Api Gateway Integration Request How to append a property to request body?](https://stackoverflow.com/questions/46407161/aws-api-gateway-integration-request-how-to-append-a-property-to-request-body) – Vijayanath Viswanathan Sep 28 '17 at 12:39
  • 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 Sep 28 '17 at 12:52
  • 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 http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html .Alternatively you can use other npm packages like 'querystring' (https://nodejs.org/api/querystring.html) to get query string, which is not official. – Vijayanath Viswanathan Sep 28 '17 at 13:06

1 Answers1

22

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

Noel Llevares
  • 15,018
  • 3
  • 57
  • 81