0

I have 2 aws lambda functions i.e. "LambdaChain1" and "LambdaChain2" i am calling aws lambda function "LambdaChain2" from aws lambda function "LambdaChain1" as follows :

enter code here var start=new Date(); enter code hereconsole.log('Loading function');

exports.handler = function(event, context) {
    //console.log('Received event:', JSON.stringify(event, null, 2));
    event.Records.forEach(function(record) {
        // Kinesis data is base64 encoded so decode here
        var payload = new Buffer(record.kinesis.data, 'base64').toString('ascii');
        console.log('Decoded payload:', payload);
    });
    context.succeed("Successfully processed " + event.Records.length + " records.");
    var params = {
  FunctionName: 'LambdaChain2', /* required */
  InvokeArgs: start.getTime() /* required */
};
lambda.invokeAsync(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
};

Now we can see from the code that I have passed "start.getTime()" as parameter to aws lambda function "LambdaChain2"....

I want to know that in aws lambda function LambdaChain2 how can I retrieve or use the parameter that I have passed to the function "LambdaChain2" from lambda function "LambdaChain1".....Can you please provide an example

1 Answer 1

2

It looks like invokeAsync is deprecated, so I used invoke for this example, but it is fairly similar.

The Payload argument in the invoke params becomes the event parameter in ChainFunc2.

ChainFunc1

var AWS = require("aws-sdk");

exports.handler = function(event, context) {
    console.log('Received event:', JSON.stringify(event, null, 2));
    var params = {
        FunctionName: "ChainFunc2",
        InvocationType: "RequestResponse",
        Payload: JSON.stringify({"greeting": "Hello, Lambda"})
    };
    var lambdaClient = new AWS.Lambda();
    lambdaClient.invoke(params, function(err, data) {
        if (err) {
            console.log("invoke failed:" + err, err.stack);
            context.fail(err);
        } else {
            console.log("invoke succeeded", data);
            context.succeed(data);
        }
    });
};

ChainFunc2

exports.handler = function(event, context) {
    console.log("Received event:", JSON.stringify(event, null, 2));
    console.log("Greeting:", event.greeting);
    context.succeed({"message": "ChainFunc2 processed this", "payload": event});
};
Sign up to request clarification or add additional context in comments.

8 Comments

Your solution is working when i am using the parameter of ChainFunc1 in ChainFunc2....But now I am not able to use the same in ChainFunc3,4,5...Basically I want to use the same parameter with same value in other functions also....I can see from the logs that JSON.stringify(event,null,2) returns null from ChainFunc3 & returns not null in 1 & 2.....How can I be able to use the parameter greeting in another lambda functions?? Do I have to make separate approach if I am using Long or Integer values in node js
How did you pass the parameter from ChainFunc2 to ChainFunc3, 4, etc? You will have to keep passing the same values along.
I want to pass milliseconds throughout my lambda function. This is my code for ChainFunc1 :: var AWS = require("aws-sdk"); var starttime=new Date().getTime(); exports.handler=function(event,context) { var payload=new Buffer(record.kinesis.data, 'base64').toString('ascii'); var params = { FunctionName: "ChainFunc2", InvocationType: "RequestResponse", Payload: JSON.stringify({"starttime": start.getTime()}) }; var lambdaClient = new AWS.Lambda();
lambdaClient.invoke(params, function(err, data) { if (err) { console.log("invoke failed:" + err, err.stack); context.fail(err); } else { console.log("invoke succeeded", data); context.succeed(data); } }); }); };
Now this the code for my lambda function ChainFunc2 :: var AWS = require("aws-sdk"); console.log("Received event:", JSON.stringify(event, null, 2)); exports.handler = function(event, context) { console.log("Received event:", JSON.stringify(event, null, 2)); var startTime = event.starttime; var params = { FunctionName: "ChainFunc3", InvocationType: "RequestResponse", Payload: JSON.stringify({"starttime": starttime}) }; var lambdaClient = new AWS.Lambda();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.