Tzvetlin Velev
Tzvetlin Velev

Reputation: 2027

Getting response from lambda via callback and letting lambda continue working

lets say I have this lambda function:

exports.handler = (event, context, callback) => {
  lambda.invoke({
    FunctionName: 'longCheckout',
    InvocationType: 'Event',
    Payload: JSON.stringify(event, null, 2) // pass params
  }, function(error, data) {

    console.log(error, data)
    callback(null, data);
  });
};

and I want to call the longCheckout lambda that will do some work and send back a response to the invoking lambda and then continue to execute a longer request. So the lambda function I am calling has a callback argument but when I call that passed in function, nothing happens in the invoking lambda. Here is what the lambda I am calling looks like

exports.handler = (event, test, callback) => {
  docClient.update(params, function(err, data) {
    callback(....)
    // continue to execute 
  })
}

Upvotes: 1

Views: 551

Answers (1)

Ronyis
Ronyis

Reputation: 1953

Lambda can be invoked synchronously (the default behavior that you used), or asynchronously. If you will choose to invoke asynchronously, both lambdas will continue running at the same time, but you won't be able to get any return value.

A possible solution to do what you ask for is:

  1. Lambda 1 invokes synchronously Lambda 2
  2. Lambda 2 calls docClient.update and invokes asynchronously Lambda 3 (then returns).
  3. Lambda 3 makes the rest of the needed execution.

Read this about asynchronous invocations.

Upvotes: 1

Related Questions