1
votes

I have created a Table inside a local DynamoDB instance and in AWS using:

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

AWS.config.update({
  region: "us-west-2"
  // Uncomment the following line to create the table locally.
  //endpoint: "http://localhost:8000"
});

var dynamodb = new AWS.DynamoDB();

var params = {
    TableName : "ProductView",
    KeySchema: [       
        { AttributeName: "id", KeyType: "HASH"},  //Partition key
        { AttributeName: "description", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "id", AttributeType: "N" },
        { AttributeName: "description", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

I can add a new item to the local and remote tables executing this:

console.log('Loading function');
var AWS = require("aws-sdk");

AWS.config.update({
    region: "us-west-2"
    // Uncomment the following line to add the item locally.
    //endpoint: "http://localhost:8000"
});

var docClient = new AWS.DynamoDB.DocumentClient();

var table = "ProductView";



var id = 1;
var description = "This is only a test";

var params = {
    TableName: table,
    Item: {
        "id": id,
        "description": description
    }
};

console.log("Adding a new item...");
docClient.put(params, function (err, data) {
    if (err) {
        console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Added item:", JSON.stringify(data, null, 2));
    }
});

However, if I use that code inside a Lambda as:

console.log('Loading function');
var AWS = require("aws-sdk");

var docClient = new AWS.DynamoDB.DocumentClient();

var table = "ProductView";

exports.handler = async (event, context) => {

    var id = 1;
    var description = "This is only a test";

    var params = {
        TableName: table,
        Item: {
            "id": id,
            "description": description
        }
    };

    console.log("Adding a new item...");
    docClient.put(params, function (err, data) {
        if (err) {
            console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
        } else {
            console.log("Added item:", JSON.stringify(data, null, 2));
        }
    });
    

}

The item won't get added and neither of the logging statements inside the callback get printed.

The item will be added in the AWS table if use this code inside the Lambda though:

console.log('Loading function');
var AWS = require("aws-sdk");

var docClient = new AWS.DynamoDB.DocumentClient();

var table = "ProductView";

exports.handler = async (event, context) => {

    var id = 1;
    var description = "This is only a test";

    var params = {
        TableName: table,
        Item: {
            "id": id,
            "description": description
        }
    };

    console.log("Adding a new item...");
    await docClient.put(params).promise();

}

Why does the callback approach work in the local DynamoDB Table but not inside the Lambda against the Table in the cloud?

1
When you are executing the above two codes (lambda functions) on the AWS server, I think they must have produced an error status code because lambda function must return a response (callback) - Sai Sreenivas
They don't. This is the output from the second version(with the await) after adding successfully the item to the table: START RequestId: 5a67c2c1-6e72-4023-925a-a69215d64b7c Version: $LATEST 2020-07-14T03:03:14.670Z 5a67c2c1-6e72-4023-925a-a69215d64b7c INFO Adding a new item... END RequestId: 5a67c2c1-6e72-4023-925a-a69215d64b7c REPORT RequestId: 5a67c2c1-6e72-4023-925a-a69215d64b7c Duration: 663.33 ms Billed Duration: 700 ms Memory Size: 128 MB Max Memory Used: 84 MB Init Duration: 380.36 ms - E.T.
So are you invoking this function from other lambda function (or) from the front end? - Sai Sreenivas
From neither of those cases, @SaiSreenivas. It gets triggered by a DynamoDB Table Stream. - E.T.
Ok once put a console.log() statement after put statement in the code and then check whether it's printing or not. - Sai Sreenivas

1 Answers

0
votes

If you want to use callback then you need to use callback style lambda handler and invoke the handler callback in the callback from the put. It doesn't matter that it works locally without it, it's free to do whatever it wants given the wrong code.