Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Chaining Lambdas Best Practices

10/27/2018

0 Comments

 
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!

Lambda.invoke Syntax

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.
const AWS = require('aws-sdk');

exports.handler = (event, context, callback) => {

  var lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});

  var params = {
    FunctionName: 'paypal-payout-impatient-deploy-dev-hello',
    InvocationType: 'RequestResponse',
    Payload: JSON.stringify({'email': 'jim-buyer@fleetwit.com', 'amount': 1.00, 'currency': 'USD'})
  }
  lambda.invoke(params, function(err, data){
    console.log(err);
    console.log('here');
  }).promise().then(data=> { callback(null, {message:'done'}); });

};
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. 

Invoking Though API Gateway Is Extra Bad

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.

Dynamo Triggers

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?

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! 
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023