AWS Lambda get context message

一个人想着一个人 提交于 2019-12-12 15:51:25

问题


I am using the test function from 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 as follows:

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 receive the message from context.done(null, 'Message') as well...

Any idea how to get the message?


回答1:


As Eric mentioned, currently Lambda doesn't offer a REST endpoint to run the function and return its result, but may in the future.

Right now, your best bet would be to use a library like lambdaws, which wraps the function deployment and execution for you and handles returning results via an SQS queue. If you'd like more control by rolling your own solution, the process is straightforward:

  1. Create an SQS queue
  2. Have your Lambda function write its result to this queue
  3. In your client, poll the queue for a result



回答2:


You are calling invokeAsync, so your Lambda function is run asynchronously. This mean you get the success means back at the point your function is successfully started, and not after it completes.

As of this writing, AWS Lambda does not yet offer a way to invoke a function synchronously, returning information from the function directly back to the caller. However, this appears to be a common request and Amazon has publicly stated they are considering the feature.



来源:https://stackoverflow.com/questions/27893615/aws-lambda-get-context-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!