2

I am wanting to pass a query parameter from API Gateway into AWS Lambda but I am always receiving null values.

Here's my Lambda function which I merely want to return the value of http://foo.bar?name=Dan

'use strict';

exports.handle = (context, event, callback) => {
  callback(null, event.name);
}

In API Gateway I have done the following:

  1. Create a Resource
  2. Create a Method (GET)
  3. Selected the correct Lambda function
  4. Selected my GET method and clicked on Integration Request
  5. Selected Body Mapping Templates
  6. Set Content-Type to application/json
  7. Added {"name": "$input.params('name')" }
  8. Save and deploy!

However, when I load up my API the value of event.name is always null. Accessing the API is done via ...amazonaws.com/beta/user?name=dan

Edit: I've tried the accepted answer here but after simply returning the event in the callback, I only receive this data:

{
  "callbackWaitsForEmptyEventLoop": true,
  "logGroupName": "",
  "logStreamName": "",
  "functionName": "",
  "memoryLimitInMB": "",
  "functionVersion": "",
  "invokeid": "",
  "awsRequestId": "",
  "invokedFunctionArn": ""
}

I have omitted the values.

2 Answers 2

3

The function arguments' placement for context and event are misplaced. Change their placement as below

'use strict';

exports.handle = (event, context, callback) => {
   callback(null, event.name);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good spot mate :). Wanted to give 10 upvote but it allow only one :)
2

Even I had the same issue before and I have modified body mapping template like below. Please try it out.

#set($inputRoot = $input.path('$'))
{    
"name" : "$input.params('$.name')"
}

If you are using path parameter then please try below,

#set($inputRoot = $input.path('$'))
{
"name" : "$input.path('$.name')"
}

11 Comments

I still get a null value when using event.name in my callback function.
Are you using path parameter or query string?
I am trying to access the variable from a query string so /beta/user?name=Dan - I would like to access name
Can you please write a log for 'event' in Lambda? Just to see the data
I am not 100% sure what that means, I'm very new to AWS! If I use callback(event) then I get this response - qwjkvridyc.execute-api.eu-west-1.amazonaws.com/beta/…
|

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.