node js invoke lambda function code example

Example 1: invoke lambda nodejs

var AWS = require('aws-sdk');
AWS.config.region = 'eu-west-1';
var lambda = new AWS.Lambda();

exports.handler = function(event, context) {
  var params = {
    FunctionName: 'Lambda_B', // the lambda function we are going to invoke
    InvocationType: 'RequestResponse',
    LogType: 'Tail',
    Payload: '{ "name" : "Alex" }'
  };

  lambda.invoke(params, function(err, data) {
    if (err) {
      context.fail(err);
    } else {
      context.succeed('Lambda_B said '+ data.Payload);
    }
  })
};

Example 2: aws lambda function setup for node js

// first install serverless
$  npm install -g serverless
// create basic template
$  serverless create --template aws-nodejs --name candidate
// configure aws and genrate client id and client secret
serverless config credentials --provider aws --key <your_access_key_id> --secret <your_access_key_secret>
 // deploy
  serverless deploy