[英]UpdateItem in AWS DynamoDB using AWS Lambda (node)
我正在尝试使用Lambda(节点)更新DynamoDB中的记录。 我可以改变params
的结构并得到像Expected params.ExpressionAttributeValues[':done'] to be a structure
,所以我相信它正在与DynamoDB进行通信。
这是参数:
{
"TableName": "test_table",
"Key": {
"id": {
"S": "90c31f23-96e3-4d5d-b08d-95aafb9bed2e"
}
},
"UpdateExpression": "SET done = :done",
"ExpressionAttributeValues": {
":done": {
"S": "t"
}
},
"ReturnValues": "UPDATED_NEW"
}
从那里它只是超时,所以很难知道问题是什么。
这是完整的lambda函数:
var aws = require('aws-sdk');
var dynamodb = new aws.DynamoDB({
apiVersion: '2012-08-10',
accessKeyId: 'xxx',
secretAccesskey: 'xxx',
region: 'us-west-2'
})
exports.handler = (event, context, callback) => {
console.log('Incoming: ', event);
var table = "test_table";
event.Records.forEach((record) => {
console.log('DynamoDB Record: %j', record.dynamodb);
var params = {
TableName: table,
Key: {
"id": record.dynamodb.Keys.id
},
UpdateExpression: "SET done = :done",
ExpressionAttributeValues: {
":done": { "S": "t" }
},
ReturnValues: "UPDATED_NEW"
};
console.log("params: %j", params);
dynamodb.updateItem(params, function(err, data) {
if (err) console.log("Unable to update item. Error: ", JSON.stringify(err, null, 2));
else console.log("Updated item succeeded: ", JSON.stringify(data, null, 2));
});
});
callback(null, "Successfully processed ${event.Records.length} records.");
};
根据文档,您必须拥有一个文档客户端,并优化您的流量控制..
var aws = require('aws-sdk');
var dynamodb = new aws.DynamoDB({
apiVersion: '2012-08-10',
accessKeyId: 'xxx',
secretAccesskey: 'xxx',
region: 'us-west-2'
})
exports.handler = (event, context, callback) => {
console.log('Incoming: ', event);
var table = "test_table";
var docClient = new dynamodb.DocumentClient()
function async(record, next) {
var params = {
TableName: table,
Key: {
"id": record.dynamodb.Keys.id
},
UpdateExpression: "SET done = :done",
ExpressionAttributeValues: {
":done": { "S": "t" }
},
ReturnValues: "UPDATED_NEW"
};
console.log("params: %j", params);
docClient.updateItem(params, function(err, data) {
if (err) console.log("Unable to update item. Error: ", JSON.stringify(err, null, 2));
else console.log("Updated item succeeded: ", JSON.stringify(data, null, 2));
next() // modify for err handling
});
}
function final() { callback(null, "Successfully processed ${event.Records.length} records."); }
var results = [];
event.Records.forEach(function(item) {
async(item, function(){
results.push(true);
if(results.length == event.Records.length) final();
});
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.