I am a complete noob to node.js. I am trying to add an item to an AWS dynamoDB table as well as return a string to Twilio. I think my Lambda function is ending before the dynamoDB entry has been written. I get the string returned to Twilio correctly but no item in the DB. I do see in the Cloudwatch logs I see the string "putItem Function Called".
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;
}
Any help is greatly appreciated.
Here are the Cloudwatch logs:
2016-12-22T01:15:07.503Z undefined Loading function
START RequestId: 134fda9f-c7e4-11e6-80bc-25be850f5913 Version: $LATEST
2016-12-22T01:15:07.522Z 134fda9f-c7e4-11e6-80bc-25be850f5913 putItem Function Called
2016-12-22T01:15:07.522Z 134fda9f-c7e4-11e6-80bc-25be850f5913 Data:
{ "param": { "S": "inboundSmsLastNumber" }, "result": { "S": "+18885551212" }, "last_updated": { "S": "2016/11/22 1:15:7.522 UTC" } }
2016-12-22T01:15:08.123Z 134fda9f-c7e4-11e6-80bc-25be850f5913 { ToCountry: 'US', ToState: '', SmsMessageSid: 'SM15398089fc1dbaeedb56e560face6380', NumMedia: '0', ToCity: '', FromZip: '66610', SmsSid: 'SM15398089fc1dbaeedb56e560face6380', FromState: 'KS', FromCity: 'TOPEKA', Body: 'Test+65', FromCountry: 'US', To: '+1**********', ToZip: '', NumSegments: '1', MessageSid: 'SM15398089fc1dbaeedb56e560face6380', AccountSid: 'AC********************************', From: '+1**********', ApiVersion: '2010-04-01' }
END RequestId: 134fda9f-c7e4-11e6-80bc-25be850f5913
REPORT RequestId: 134fda9f-c7e4-11e6-80bc-25be850f5913 Duration: 838.90 ms Billed Duration: 900 ms Memory Size: 128 MB Max Memory Used: 21 MB
REPORT RequestId: ef2b5301-c7e9-11e6-99a1-f53533ec880e Duration: 192.71 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 18 MB No newer events found at the moment. Retry.
– wkl