无法使用node.js和Lambda编写AWS dynamoDB

时间:2016-12-22 01:30:32

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

我是node.js的完整菜鸟。我正在尝试将项添加到AWS dynamoDB表以及将字符串返回到Twilio。我认为我的Lambda函数在编写dynamoDB条目之前就已经结束了。我正确地将字符串返回到Twilio但DB中没有项目。我确实在Cloudwatch日志中看到了字符串" putItem函数被调用"。

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

var dynamoDBConfiguration = {"region": "us-east-2"};
AWS.config.update(dynamoDBConfiguration);
var dd = new AWS.DynamoDB();
var tableName = 'kta';

console.log('Loading function');

exports.handler = function(event, context) {
//*****************************************
  try {
    putItem = function(param,result) {
        console.log(" putItem Function Called");
        var d = new Date();
        var last_updated = d.getUTCFullYear() + "/" + d.getUTCMonth() + "/" + d.getUTCDate() + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds() + "." + d.getUTCMilliseconds() + " UTC";
         var item = {
            'param': { 'S': 'inboundSmsLastNumber' },
            'result': { 'S': '+18885551212'},
            'last_updated': { 'S': last_updated }
          };

          console.log("Data: %j",item);
          var response = dd.putItem({
             'TableName': tableName,
             'Item': item
          }, function(err, data) {
              if (err) {
                context.fail("Error in putItem "+err);
              } else {
                context.succeed("Successfully Inserted");
              }
          });
        };

    putItem();

  } catch (error) {
    context.fail("Caught: " + error);
  }

//*****************************************
    qryObject = parseQuery(event.reqbody);
    console.log(qryObject);

// Send SMS with the inbound SMS information    
        context.succeed("<Sms from='+18885551212' to='+19991112222'>You received an SMS from " + qryObject.From + "</Sms>");

};

function parseQuery(qstr) {
        var query = {};
        var a = qstr.substr(0).split('&');
        for (var i = 0; i < a.length; i++) {
            var b = a[i].split('=');
            query[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
        }
        return query;
}

非常感谢任何帮助。

以下是Cloudwatch日志:

2016-12-22T01:15:07.503Z undefined加载功能

START RequestId:134fda9f-c7e4-11e6-80bc-25be850f5913版本:$ LATEST

2016-12-22T01:15:07.522Z 134fda9f-c7e4-11e6-80bc-25be850f5913 putItem函数被调用

2016-12-22T01:15:07.522Z 134fda9f-c7e4-11e6-80bc-25be850f5913数据:

{     &#34; param&#34;:{         &#34; S&#34;:&#34; inboundSmsLastNumber&#34;     },     &#34;结果&#34;:{         &#34; S&#34;:&#34; + 18885551212&#34;     },     &#34; last_updated&#34;:{         &#34; S&#34;:&#34; 2016/11/22 1:15:7.522 UTC&#34;     } }

2016-12-22T01:15:08.123Z 134fda9f-c7e4-11e6-80bc-25be850f5913 {ToCountry:&#39; US&#39;,ToState:&#39;&#39;,SmsMessageSid:&#39 ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FromState:&#39; KS&#39;,FromCity:&#39; TOPEKA&#39;,Body:&#39; Test + 65&#39;,FromCountry:&#39; US&#39;,To:&# 39; +1 **********&#39;,ToZip:&#39;&#39;,NumSegments:&#39; 1&#39;,MessageSid:&#39; SM15398089fc1dbaeedb56e560face6380&#39 ;,AccountSid:&#39; AC ********************************&#39;,来自:&# 39; +1 **********&#39;,ApiVersion:&#39; 2010-04-01&#39; }

END RequestId:134fda9f-c7e4-11e6-80bc-25be850f5913

REPORT RequestId:134fda9f-c7e4-11e6-80bc-25be850f5913持续时间:838.90毫秒 计费时长:900毫秒内存大小:128 MB最大使用内存:21 MB

2 个答案:

答案 0 :(得分:1)

您不允许发电机请求完成。在处理程序的末尾,您调用:

   context.succeed("<Sms from='+18885551212' to='+19991112222'>You received an SMS from " + qryObject.From + "</Sms>");

然后处理程序返回并且lambda停止运行。发电机呼叫已经排在上面,但它还没有时间运行。

让发电机的回调执行context.succeed,你会确定它在发电机完成后触发。

答案 1 :(得分:0)

要设置您的dynamoDB,请尝试使用:

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

AWS.config.update({
  region: "us-east-2",
});

var dd = new AWS.DynamoDB.DocumentClient();
var tableName = 'kta';

对于put-item请求,请尝试:

var params = {
    TableName: tableName,
    Item: {
        'param': { 'S': 'inboundSmsLastNumber' },
        'result': { 'S': '+18885551212'},
        'last_updated': { 'S': last_updated }
    }
};

dd.put(params, function(err, data) {
    if (err) {
        context.fail("Error in putItem "+err);
    } else {
        context.succeed("Successfully Inserted");
    }
});

有关详细信息,请按照以下网址中的说明进行操作:http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.01