Search code examples
javascriptamazon-s3amazon-dynamodbaws-sdkalexa-skills-kit

AWS Lambda not running DynamoDB commands: getItem or batchGetItem


I am trying to get the batchGetItem() DynamoDB function to return values from my table. However, when called, it doesn't execute the code. I tested this by adding logging to various sections of the code. I ripped out the meat, but this is the code being executed (storageEvents.js):

'use strict';
var AWS = require("aws-sdk");

var storageEvents = (function () {
    var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10', region: 'us-east-1'});
    var s3 = new AWS.S3({apiVersion: '2012-08-10', region: 'us-east-1'});

    var myBucket = 'my.events';
    var myObject = 'events.txt';

    return {
        loadEvents: function (session, callback) {
            console.log('loadEvents');
            dynamodb.batchGetItem({
                RequestItems: {
                  'EventsData': {
                    Keys: [
                      {'country': {S: 'USA'}}
                    ],
                  }
                }
            }, function (err, data) {
                console.log('batchGetItem');
                var currentEventsList = "test";
                callback(currentEventsList);
            });
        },
        loadEvent: function(session, callback) {
            console.log('loadEvent');
            dynamodb.getItem({
                TableName: 'EventsData',
                Key: { event_name: { S: 'Fun Event' } }
            }, function (err, data) {
                console.log('getItem');
                var currentEventsList = "test";
                callback(currentEventsList);
            });
        },
        loadS3events: function(session, callback) {
            console.log('loadS3events');
            s3.getObject({
                Bucket: myBucket,
                Key: myObject
            }, function(err, data) {
                console.log('getObject');
                var currentEventsList = "test";
                callback(currentEventsList);
            });
        }
    };
})();
module.exports = storageEvents;

This is how it is being called from index.js:

var storageEvents = require('./storageEvents');

storageEvents.loadEvents(session, function (currentEventsList) {
    console.log("getEventsFromDynamoDb currentEventsList: " + currentEventsList);
    eventList = currentEventsList;
});

The other functions are called similarly. When this executes, I see in the CloudWatch logs the 'loadEvents', but I never see 'batchGetItem'. None of my attempts at making this work have been successful for DynamoDB or S3, so I have to be doing something wrong. I know my permissions are correct for at least DynamoDB because a separate execution of a load function for user information in a different table works fine. I modeled this after that, but since I'm accessing them differently, I'm not sure what I'm doing wrong.


Solution

  • I figured out the problem. I had been calling storageEvents.loadEvents() within a function, and I was calling that function from the handleFirstEvent() function. Since it wasn't being done with a callback, the function was quitting before it received the data. In my loadUser() function, this was working because I was using a callback from the main handler function. I now understand that callbacks must be stacked in order to be able to use the data.