Welp, we're going to look at some nodejs code for and aws lambda function here, and specifically the code will be invoking a different lambda function. Although there are a few ways to do this, I really wanted to find a way of chaining lambdas such that they don't have to wait for the one one they just called to finish. It's a bit confusing, but maybe with some example you'll understand what I mean. Well then let's dive right in!
Here's an example where we are using the 'aws-sdk' library, specifically AWS.Lambda. This allows us to use the invoke function, simple enough, to invoke the function.
So if this is so easy and straightforward then aren't we done here? Why to we need other ways of calling the function? Well, the thing that I don't like about calling your lambdas this way is that you have this open promise waiting to be fulfilled until the second lambda resolves. You could fire off the promise and then immediately short circuit the lambda, but this feels to me pretty hackish and filled up my logs with tons of false positive error messages.
Just wanted to make it totally clear that invoking another lambda via a REST endpoint (get or post request) doesn't help anything. It's still expecting some asynchronous request to finish, and so the original lambda will have a wait time at least as long as the second lambda. So, it doesn't solve the original problem of gracefully chaining lambdas, but it's actually even worse than the lambda.invoke approach because here you'll have extra network load, latency, and aws fees for using api gateway when you don't really need to.
This is ultimately the method we ended up using for the awesome trivia gambling app and many others following it. Basically, you have the initial lambda, it's finished with it's logic, so then it writes a certain object to a specific dynamoDb table, and then when that dynamo write returns that it completed successfully this original lambda terminates gracefully. You have the second lambda configured with a dynamodb trigger pointed at that specific dynamo table so that when objects are inserted, updated, deleted, etc. then the second lambda will be invoked, and it will have the new document in the dynamo table as one means of passing parameters between the functions.
What do you think about chaining lambdas with dynamo triggers? Have you come across any other ways to chain lambda functions in such a way that they can gracefully complete and not wait for the child promises to resolve? I'd love to hear your suggestions and questions in the comments below, and you can always tweet to me also at @webWhizJim. Happy coding!
The posts on this site are written and maintained by Jim Lynch. About Jim...