AWS Lambda receives context message

I am using a test function from the AWS console:

console.log('Loading event');

exports.handler = function(event, context) {
    console.log('value1 = ' + event.key1);
    console.log('value2 = ' + event.key2);
    console.log('value3 = ' + event.key3);
    context.done(null, 'Hello World');  // SUCCESS with message
};

      

And calling it in nodejs like this:

var params = {
  FunctionName: 'MY_FUNCTION_NAME', /* required */
  InvokeArgs: JSON.stringify({
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
  })
};

lambda.invokeAsync(params, function(err, data) {
  if (err) {
    // an error occurred
    console.log(err, err.stack);
    return cb(err);
  }
  // successful response
  console.log(data);
});

      

and everything works fine:

//Console Output
{ Status: 202 }

      

But I was expecting to get a message from context.done (null, 'Message') as well ...

Any idea how to get the message?

+3


source to share


2 answers


As Eric pointed out, Lambda does not currently offer a REST endpoint to run a function and return its result, but it may in the future.

Right now, your best bet is to use a library like lambdaws that completes the deployment and execution of the function for you and handles the return of the results through an SQS queue. If you need more control by copying your own solution, the process is simple:



  • SQS queue creation
  • Let your Lambda function write the result to this queue
  • In your client, poll the queue for the result
+4


source


You are calling invokeAsync, so your lambda function runs asynchronously. This means that you get success by returning the moment your function has successfully run, not after it completes.



As of this post, AWS Lambda does not yet offer a way to call a function synchronously by returning information from the function directly back to the caller. However, this appears to be a general request and Amazon has publicly stated that it is considering this feature.

+1


source







All Articles