Node.js DynamoDB文档客户端put方法在本地DynamoDB实例上工作,但在AWS Lambda中不工作
我在本地DynamoDB实例和AWS中创建了一个表,使用:Node.js DynamoDB文档客户端put方法在本地DynamoDB实例上工作,但在AWS Lambda中不工作,node.js,aws-lambda,amazon-dynamodb,Node.js,Aws Lambda,Amazon Dynamodb,我在本地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 dynamod
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));
}
});
}
该项不会被添加,回调中的日志语句也不会被打印
如果在Lambda中使用此代码,则该项目将添加到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表中工作,而不是在Lambda中针对云中的表工作?如果要使用回调,则需要使用回调样式的Lambda处理程序,并从put调用回调中的处理程序回调。不管它是否在本地工作,只要代码错误,它就可以自由地执行任何操作。如果要使用回调,则需要使用回调样式的lambda处理程序,并从put调用回调中的处理程序回调。如果没有它,它在本地工作并不重要,只要代码错误,它就可以自由地执行任何它想做的事情。当您在AWS服务器上执行上述两个代码(lambda函数)时,我认为它们一定产生了错误状态代码,因为lambda函数必须返回响应(回调),而它们没有。这是成功将项目添加到表中后第二个版本(带有等待)的输出:
START RequestId:5a67c2c1-6e72-4023-925a-a69215d64b7c版本:$LATEST 2020-07-14T03:03:14.670Z 5a67c2c1-6e72-4023-925a-a69215d64b7c信息添加新项目。。。结束请求ID:5a67c2c1-6e72-4023-925a-a69215d64b7c报告请求ID:5a67c2c1-6e72-4023-925a-a69215d64b7c持续时间:663.33毫秒计费持续时间:700毫秒内存大小:128 MB最大使用内存:84 MB初始持续时间:380.36毫秒
那么您是否从前端从其他lambda函数(或)调用此函数?从这两种情况都没有,@SaiSreenivas。它由DynamoDB表流触发。确定在代码中的put
语句之后放置console.log()
语句,然后检查是否正在打印。在AWS服务器上执行上述两个代码(lambda函数)时,我认为它们一定产生了一个错误状态代码,因为lambda函数必须返回一个它们没有返回的响应(回调)。这是成功将项目添加到表中后第二个版本(带有等待)的输出:START RequestId:5a67c2c1-6e72-4023-925a-a69215d64b7c版本:$LATEST 2020-07-14T03:03:14.670Z 5a67c2c1-6e72-4023-925a-a69215d64b7c信息添加新项目。。。结束请求ID:5a67c2c1-6e72-4023-925a-a69215d64b7c报告请求ID:5a67c2c1-6e72-4023-925a-a69215d64b7c持续时间:663.33毫秒计费持续时间:700毫秒内存大小:128 MB最大使用内存:84 MB初始持续时间:380.36毫秒
那么您是否从前端从其他lambda函数(或)调用此函数?从这两种情况都没有,@SaiSreenivas。它由DynamoDB表流触发。确定后,将console.log()
语句放在代码中的put
语句之后,然后检查它是否正在打印。