0
var aws = require('aws-sdk');
var lambda = new aws.Lambda({
    region: 'us-west-2' //change to your region
});

lambda.invoke({
    FunctionName: 'my-function',
    Payload: JSON.stringify(event, null, 2) // pass params
}, function(error, data) {
    if (error) {
        context.done('error', error);
    }
    if(data.Payload){
        context.succeed(data.Payload)
    }
});

How can I wait for the code above to finish, before running the code below.

Also I would like to know if the code above failed.

const response = {
    statusCode: 200,
    body: JSON.stringify('2 Lambda Done'),
};
return response;
skirtle
  • 22,128
  • 2
  • 23
  • 49
Jay
  • 93
  • 8

2 Answers2

0

In the case of synchronous invocation, you don't have to do anything special. You just invoke one lambda function from inside of the other. In that case, your invoking function will wait until the invoked function completes. The output will be provided in the response object.

For asynchronous invocation, a redesign of your current functions is required. Since your question does not specify that you are interested in asynchronous invocation, there is not much sense on providing more info about that, as more details from you would be required to provide a concise answer.

Mayur
  • 2,186
  • 2
  • 17
  • 32
Marcin
  • 108,294
  • 7
  • 83
  • 138
0

Previous question with answers that will help: Can an AWS Lambda function call another

In a nutshell:

The more scalable approach, and most often seen in AWS, is using SNS and generally you'll find people regard having one lambda call another as an anti-pattern since the first lambda's execution time has to include the execution time of the second lambda.

If you're billing for two lambdas at once, why not have the second part of the first or use the SNS approach. You'd have Lambda1 send whatever payload to SNS and then it would trigger Lambda2, instead of having your Lambda1 process some function after the response, your Lambda2 would then process whatever "success" or "failed" operation you need or trigger another SNS to handle that.

Or, lastly, you could use Step Functions.

Then again, there is this blog post I like which discusses another theory: https://winterwindsoftware.com/call-lambda-function-from-another/

GMaiolo
  • 3,255
  • 1
  • 16
  • 33
dalumiller
  • 61
  • 1
  • 2