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?

3
  • 1
    Possible duplicate of Aws Api Gateway Integration Request How to append a property to request body? Commented Sep 28, 2017 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? Commented Sep 28, 2017 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 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. Commented Sep 28, 2017 at 13:06

1 Answer 1

23

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.