0

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

2
  • Can you print the log output from your lambda function? Commented Dec 22, 2016 at 1:59
  • What do your Cloudwatch logs show for the last line of each execution? It should say something like 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. Commented Dec 22, 2016 at 2:02

2 Answers 2

1

You're not allowing the dynamo request to finish. At the end of the handler, you call:

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

The handler then returns and the lambda stops running. The dynamo call has been enqueued above, but it hasn't had the time to run.

have the callback from dynamo perform the context.succeed, and you'll be sure it triggers after the dynamo complets.

Sign up to request clarification or add additional context in comments.

Comments

0

To setup your dynamoDB, try using:

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

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

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

And for the put-item request, try:

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

For more information, you can follow the instructions at: http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.01

3 Comments

as far as I can tell, you never need to set the region in dynamo unless you want to make a cross-region request
filipebarretto, your solution does not seem to resolve the issue. Same symptoms as before, response gets back to Twilio fine but still no item updated in dynamoDB.
Thanks Dan, I removed the AWS.config.update statement.

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.