Tuesday, June 26, 2018

Call an AWS Lambda Function from another Lambda Function

AWS Lambda is I think one of the best additions to AWS, however it does have some issues becuase the architecture has changed over the years and one such problem the default VPC that it runs in. Default configuration runs great, but if you want better security, then not so much. But I digress. One of the most compelling features of Lambda is that you don't have to manager the VM or Docker Container. However because of that you might want to break up the responsibility between multiple Lambda functions, and in this case one Lambda will most certainly want to call another Lambda. So here's how you do that:

const aws = require('aws-sdk');
const lambda = new aws.Lambda({region: 'us-west-1'});

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


Note: I really appreciate Lambda. But to be honest they are difficult to use. There is a time and a place to use them and it should be in every AWS developer's bag of tricks. The real trick I alluded to above is the VPC, but that's for a proper DevOps person to discuss the nuances of that. Also know that while in small uses Lambda is cost effective there is a cross over point where Lambda becomes more expensive than EC2 or Elastic Container Services (Docker).

No comments:

Post a Comment