1

How do I get the data back from a lambda invoked with as an event to the calling function?

Essentially the lambda function I have is:

exports.handler = function(event, context, callback) {
    var data = {};
    data.foo ='hello';
    callback(null, data)
}

and the invoking function looks like this:

var AWS = require('aws-sdk');

var lambda = new AWS.Lambda();
var params = {
    FunctionName: 'SomeFunction',
    InvocationType: 'Event'
};

lambda.invoke(params, function (err, data) {
    if (err) {
        console.log(err, err.stack); // an error occurred
    } else {
        console.log(JSON.stringify(data, null, 2));
    }
});

However the only thing I get back from the function is

{
  "StatusCode": 202,
  "Payload": ""
}

I thought the point of the callback parameter was to allow the invoking function to get the data when the function has finished. Am I using it wrong or is what I am asking not possible with Lambdas?

2 Answers 2

9

When you invoke the Lambda function you need to set the InvocationType to 'RequestResponse' instead of 'Event'.

When using the Event type your callback is invoked when the payload has been received by AWS's servers. When using the RequestResponse type your callback is invoked only after the Lambda function has completed and you will receive the data it provided to its callback. It is not possible to do what you want with the Event type.

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

7 Comments

Is it not possible to do it with 'Event' type? I thought the point of the callback parameter was for the invoking function to be able to get the data from the lambda when it finished executing?
Shame. You can use the optional callback to return information to the caller, otherwise return value is null, this made it sound like I could use the callback to return result of the invocation
@user2127726 I mean, you can… just use the right InvocationType. Why is changing that an issue?
RequestResponse is synchronous right? So there would have to be another thread to prevent blocking the calling one right? Just some people don't seem to like that extra step
@user2127726 no, using the RequestResponse type will not make the call synchronous.
|
0

As @MatthewPope commented in this answer, if you do need the results from an asynchronous Lambda execution, you should consider having the Lambda function write its results to S3 (or something to that effect) at a known location where clients of the Lambda function can periodically check for the existence of the result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.