我尝试使用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.");
};
答案 0 :(得分:3)
根据文档,您必须拥有一个文档客户端,并优化您的流量控制..
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();
});
});
};