0

When I have an AWS lambda finish, I want to trigger another lambda to run. The first lambda lets call X, is full of asynch code and I would rather not mess with that. I thought I could use cloudwatch to say when X is done call lambda Y. But I cannot find out how to do that.

Can someone help me figure out how to run 1 lambda when another lambda finishes? thank you very much

Edit

It has been suggested that I wrap all the asynch calls and use an SDK, it isnt feasible to re-write this code to use a new sdk. What I am looking for is a way to monitor when a lambda is done and then call another lambda. More of an observer pattern instead of a notifier pattern.

Community
  • 1
  • 1
gh9
  • 8,245
  • 8
  • 53
  • 89
  • "Your first Lambda publishes messages to your SNS Topic and the second Lambda is subscribed to this topic. " RE: https://stackoverflow.com/a/31731917/368552 – Luke Hutton Jan 09 '18 at 21:23

3 Answers3

0

Have your first Lamba send a message on SNS. Then configure CloudWatch to monitor SNS, the result being to run your second Lambda.

clay
  • 5,364
  • 1
  • 19
  • 20
  • The issue with this is, I dont know when the first lambda finishes. With all the asynch methods it could be whenever. – gh9 Jan 09 '18 at 21:18
  • You need a control flow for those async calls. See the npm package [async](https://caolan.github.io/async/) to make sure all your async calls have finished before calling a final callback (success send SNS, error stop or log). Similar control flow can be accomplished with Promises. – clay Jan 09 '18 at 21:23
  • Sounds like you have inherited a bunch of bad asynch code, looks like you should fix it – Luke Hutton Jan 09 '18 at 21:26
0

The aws-sdk allows you to instantiate a new Lambda object, and assuming you know the ARN of the second lambda you wish to execute, you can invoke that after your asynchronous code has complete.

Something similar to this for your given code in the first lambda should work...

const AWS = require('aws-sdk')
const lambda = new AWS.Lambda(/* Options object, look in docs */)

asynchronousOperations()
    .then(() => {
        lambda.invoke({
            FunctionName: SECOND_LAMBDA_ARN,
            InvocationType: TYPE,
            Payload: JSON.stringify(PAYLOAD_OBJ_IF_YOU_HAVE_ONE),
            // other options you may want
        })
    })
m_callens
  • 4,800
  • 4
  • 24
  • 42
0

You could do a quick-fix by wraping callback function to trigger your lambda.

let wrap = (originalFunction) => {
    return (event, context, callback) => {
        let overwriteCallback = (err, data) => {
            // do your stuff here
            // publish to sns to run your lambda
            // or directly run your lambda
            // then finally run callback

            callback(err, data)
        }
        originalFunction(event, context, overwriteCallback);
    }
}

module.exports = wrap((event, context, callback) => {
    //original mess we don't want to touch
})

If it is older you probably also want to overwrite context.succeed(); context.fail(); context.done()

You can find the documentation about those methods here: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-using-old-runtime.html

Vjerci
  • 425
  • 1
  • 5
  • 15