Saturday, December 5, 2020

NodeJS AWS Lamda Client

In this Post let's see how to invoke an AWS Lambda from NodeJS

In case someone wants to invoke an AWS Lambda from NodeJS (could be from Express WebApp) then we can do so using the AWS SDK for JavaScript in Node.js

The following code can be used to invoke the Lambda

  • Create a function which invokes Lambda

 let AWS = require('aws-sdk');  
 const util = require('util');  
 AWS.config.update({ region: 'us-east-1' });  
 let lambda = new AWS.Lambda();  
 //Promisify the Lambda  
 let promisifiedLambda = util.promisify(lambda.invoke.bind(lambda));  
 /**  
  * This function invokes Lambda  
  * @param {*} functionName Name of the Lambda function  
  * @param {*} payload Payload to be passed onto Lambda  
  */  
 let lambdaInvoker = async (functionName, payload) => {  
   let params = {  
     FunctionName: functionName,  
     Payload: `{ "body" : ${JSON.stringify(payload)} }`  
   }  
   let res;  
   try {  
     //Use this format if Lambds is not promisified  
     res = await lambda.invoke(params).promise();  
     //Use this format if Lambda is promisified  
     //res = await promisifiedLambda(params);  
   } catch (err) {  
     console.error(`err:: ${err}`);  
   }  
   console.log(`Lambda invocation successful`);  
   console.log(res);  
   return res.Payload;  
 }  

  • Invoking the function created above passing Lambda Name and Payload
 //Let's invoke the Lambda  
 let lambdaFunctionName = 'cli-target-lambda';  
 let payload = { name: "abc", age: 20 };
 //Invoke lambdaInvoker using IIFE  
 (async () => {  
   try {  
     let response = await lambdaInvoker(lambdaFunctionName, payload);  
     console.log(response);  
   } catch (err) {  
     console.error(`err:: ${err}`);  
   }  
 })();  

The complete source code can be found here

No comments:

Post a Comment