jose1278
jose1278

Reputation: 13

Pass Value from one Lambda to another Lambda

Lambda A:

var aws = require('aws-sdk');
var ses = new aws.SES({region: 'us-east-2'});
var lambda = new aws.Lambda({region: 'us-east-2'});


const mysql = require('mysql'); 
const connection = mysql.createConnection({ 
  host: process.env.host1,  
  user: process.env.user, 
  password: process.env.password, 
  port: process.env.port1, 
  connectionLimit: 10,   
  multipleStatements: true,
  connectionLimit: 1000,   
  connectTimeout: 60 * 60 * 1000, 
  acquireTimeout: 60 * 60 * 1000, 
  timeout: 60 * 60 * 1000, 
  debug: true 
});


exports.handler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  var rand = function() {return Math.random().toString(36).substr(2); };
  var token = function() {return rand() + rand(); };
  
  const t=token();
  const e=JSON.stringify(t);
    
  var params = {
    FunctionName: 'LambdaB',
    InvocationType:'RequestResponse',
    Payload: '{"name" : '+e+'}'
  };

  lambda.invoke(params, function(err, data) {
    if (err) {
      context.fail(err);}
    else {
      context.succeed('Lambda_B said '+ data.Payload);}
  });
  
  var query1= connection.query("SELECT * FROM users WHERE email=?",[event.email],function(error, results){
    if(results && results.length !== 0){
     var params = {
      Destination: {
        ToAddresses: [event.email]},
      Message: {
        Body: {
          Text: {Data:t}
        },
        Subject: { Data: "RESET PASSWORD TOKEN"}
      },
      Source: "Source Email"
      };

      ses.sendEmail(params, function (err, data) {
        callback(null, {err: err, data: data});
        if (err) {
          console.log(err);
          context.fail(err);}
        else{
          console.log(data);
          context.succeed(event);
        }
      });
  }});
};

Lambda B:

exports.handler = function(event, context) {
  console.log('Lambda B Received event:', JSON.stringify(event, null, 2));
  context.succeed(event.name);
  
};

Response when Lambda A is tested:

"Lambda_B said \"550n1hbz2zwq2xln4n2cd\""

Response when Lambda B is tested:

null

I am not sure I am doing this right but I am passing the payload which is a random generated token, from Lambda A to Lambda B. Basically I am creating an android app and what I want to do is process the token(which is in the payload) in Lambda B so I can verify whether the token the user enters on the console matches the token that has been generated and sent to their email. If it matches they can be sent to the next page on app where they can reset their account password. But as you can from the response below, Lambda A gives back the string token and Lambda B when tested outputs null. I want Lambda B to retrieve the token passed to it and do logic operations on that but unfortunately I don't know how to retrieve the token so could you help me to figure that out?

Upvotes: 0

Views: 852

Answers (1)

Binh Nguyen
Binh Nguyen

Reputation: 2157

I think your code needs to change from

InvocationType: RequestResponse

into

InvocationType: Event

Please find this for your reference: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html

Upvotes: 1

Related Questions