[英]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.