Я создал таблицу внутри локального экземпляра DynamoDB и в AWS, используя:
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));
}
});
Я могу добавить новый элемент в локальную и удаленную таблицы, выполнив это:
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));
}
});
Однако, если я использую этот код внутри Lambda как:
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));
}
});
}
Элемент не будет добавлен, и ни один из операторов журнала внутри обратного вызова не будет напечатан.
элемент будет добавлен в таблицу AWS, если использовать этот код внутри лямбда-выражения:
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();
}
Почему метод обратного вызова работает в локальной таблице DynamoDB, но не внутри лямбда-выражения против таблицы в облаке ?