从 LAMBDA 触发器更新 DynamoDB
Updating a DynanoDB from LAMBDA trigger
每当 table 上发生 MODIFY
事件时,我都尝试使用具有某些值的 lambda 触发器来更新我的 table,但即使触发了该函数,更新查询也会执行不是 运行 并且更改没有反映在 dynamodb 中,并且 CloudWatch
没有显示关于更新的错误日志 我已经为我的 Lambda 提供了 AmazonDynamoDBFullAccess
和 AWSLambdaDynamoDBExecutionRole
权限。代码无法更新。
var AWS = require('aws-sdk');
// extra
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event , context , callback) => {
// TODO implement
event.Records.forEach((record) => {
console.log('Stream record: ', JSON.stringify(record, null, 2));
// logic to find insert type / modify type
//if(record.eventName == 'MODIFY' || record.eventName == 'INSERT'){
if(record.eventName == 'MODIFY'){
// extract student id, and marks from event itself (save I/O)
console.log("Printing new image values",JSON.stringify(record.dynamodb.NewImage,null,2));
// modify the same record once new value is calculated
//############# code for pushing to DynamoDB #################
var params = {
TableName:'my-table name',
Key:{
"student_id": 'abc_123'
},
UpdateExpression: "set score1 = :r",
ExpressionAttributeValues:{
":r":88
},
ReturnValues:"UPDATED_NEW"
};
console.log("Updating the item...");
docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
}
});
//############# end of code ###############
}
});
console.log(event);
const response = {
statusCode: 200,
body: JSON.stringify(event),
};
return response;
};
我哪里错了?
感谢您的帮助!!
由于您使用的是 async handler 我认为问题在于您的函数在处理程序的主体有机会 运行.
之前完成
纠正此问题的一种方法是使用 Promise
技术,如 AWS docs 所示。
每当 table 上发生 MODIFY
事件时,我都尝试使用具有某些值的 lambda 触发器来更新我的 table,但即使触发了该函数,更新查询也会执行不是 运行 并且更改没有反映在 dynamodb 中,并且 CloudWatch
没有显示关于更新的错误日志 我已经为我的 Lambda 提供了 AmazonDynamoDBFullAccess
和 AWSLambdaDynamoDBExecutionRole
权限。代码无法更新。
var AWS = require('aws-sdk');
// extra
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event , context , callback) => {
// TODO implement
event.Records.forEach((record) => {
console.log('Stream record: ', JSON.stringify(record, null, 2));
// logic to find insert type / modify type
//if(record.eventName == 'MODIFY' || record.eventName == 'INSERT'){
if(record.eventName == 'MODIFY'){
// extract student id, and marks from event itself (save I/O)
console.log("Printing new image values",JSON.stringify(record.dynamodb.NewImage,null,2));
// modify the same record once new value is calculated
//############# code for pushing to DynamoDB #################
var params = {
TableName:'my-table name',
Key:{
"student_id": 'abc_123'
},
UpdateExpression: "set score1 = :r",
ExpressionAttributeValues:{
":r":88
},
ReturnValues:"UPDATED_NEW"
};
console.log("Updating the item...");
docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
}
});
//############# end of code ###############
}
});
console.log(event);
const response = {
statusCode: 200,
body: JSON.stringify(event),
};
return response;
};
我哪里错了?
感谢您的帮助!!
由于您使用的是 async handler 我认为问题在于您的函数在处理程序的主体有机会 运行.
之前完成纠正此问题的一种方法是使用 Promise
技术,如 AWS docs 所示。