6

I have successfully invoked a lambda function from within my node file. It returns 200 and success, but I need to pass in a value to the function I am calling and am unsure how to do that. I am beginning to learn lambda and believe I am not understanding correctly how some things operate. I am attempting to pass in an email address to the lambda function.

function callLambda(){
var AWS = require('aws-sdk');
AWS.config.region = 'us-west-2'

var lambda = new AWS.Lambda();
var params = {
  FunctionName: 'UploadDailyRecords', 
  Payload: '{"client_id" : "[email protected]"}'
};
lambda.invoke(params, function(err, data) {
  if (err) console.log(err, err.stack);
  else     console.log(data);         
});
}
callLambda();

Also, I am unsure of the correct format of how to invoke it from the other function to accept a variable from this invocation. This is the way the invoked lambda is set up on AWS.

exports.handler = function(event, context, callback) {
  get_clients_email();
  callback(null, "completed");
}

Thanks for any help.

1
  • What did the final solution look like here? How would it work if I need to pass to attributes in the Payload object? Commented Aug 13, 2020 at 14:18

1 Answer 1

9

Everything looks good, you just need to access the payload parameter in your Lambda function now via event.client_id.

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

9 Comments

can you elaborate? When I run this and invoke "get_clients_email();", that works, but there is nothing being passed into it. I need to pass what is in the payload to the lambda. What I am thinking you are saying is in "get_clients_email()", i should make it, "get_clients_email(event.client_id)"?
That was it. get_clients_email(event.client_id) made it work. I was able to console.log to verify the variables were being passed. Thanks!
One other thing. Now that it works, I am trying to pass in something that is not hardcoded as the payload. From my experience, it has to be in string format. I tried passing in a hash, it wouldnt accept it. I tried creating a string around it with clientstring = " ' " + clienthash + " ' " and it wouldnt accept that. Any idea on how I can pass a hash into it as its string format it seems to require?
You are currently passing a JSON string in the payload. You can't pass a variable/object/hash whatever directly, you have to convert it such that the entire payload parameter is a valid JSON string.
Yes, I'm trying to build that valid JSON string, but I am finding it very difficult. No matter how I try to build it, it is not coming out in the acceptable format. Very frustrating. Anyways, thanks for your help.
|

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.