使用node.js检索aws lambda中的函数参数值

时间:2015-11-02 14:24:58

标签: node.js amazon-web-services aws-lambda

我有2个aws lambda函数,即“LambdaChain1”和“LambdaChain2” 我从aws lambda函数“LambdaChain1”调用aws lambda函数“LambdaChain2”如下:

enter code here var start = new Date(); enter code here console.log('正在加载函数');

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
});
};

现在我们可以从代码中看到我已将“start.getTime()”作为参数传递给aws lambda函数“LambdaChain2”....

我想知道在aws lambda函数LambdaChain2中如何检索或使用我从lambda函数“LambdaChain1”传递给函数“LambdaChain2”的参数.....你能提供一个例子吗? p>

1 个答案:

答案 0 :(得分:0)

看起来invoke已被弃用,因此我在此示例中使用了Payload,但它非常相似。

invoke参数中的event参数成为ChainFunc2中的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); } }); }; 参数。

<强> ChainFunc1

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});
};

<强> ChainFunc2

DataTable newTable = new DataTable();
newTable.TableName = "<NewTableName>";
//Make a new Random generator
Random rnd = new Random();
while (<new table length> != <old table length>)
{
    //We'll use this to make sure we don't have a duplicate row
    bool rowFound = false;
    //index generation
    int index = rnd.Next(0, <max number of rows in old data>);
    //use the index on the old table to get the random data, then put it into the new table.
    foreach (DataRow row in newTable.Rows)
    {
        if (oldTable.Rows[index] == row)
        {
            //Oops, there's duplicate data already in the new table. We don't want this.
            rowFound = true;
            break;
        }
    }
    if (!rowFound)
    {
        //add the row to newTable
        newTable.Rows.Add(oldTable.Rows[index];
    }
}