Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access GET parameter in AWS Lambda

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?

like image 723
user3191334 Avatar asked Sep 28 '17 12:09

user3191334


People also ask

Can Lambda take parameters?

A Python lambda function behaves like a normal function in regard to arguments. Therefore, a lambda parameter can be initialized with a default value: the parameter n takes the outer n as a default value.


1 Answers

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

like image 185
Noel Llevares Avatar answered Oct 06 '22 00:10

Noel Llevares