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?
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.console.log()statement afterputstatement in the code and then check whether it's printing or not. - Sai Sreenivas