当您首次创建 Todo 项目时,它会作为项目存储在 DynamodDB 表中。要更新待办事项,您需要更新 DynamoDB 表中的项目。
当您更新 DynamoDB 中的项目时,您需要使用 AWS 开发工具包来处理此问题。如果您在 lambda 函数中使用 nodejs,最简单的 sdk 是使用 AWS DynamoDB Document Client。
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'}); //replace Region with the region you are using ('EU-WEST-1')
// Create DynamoDB document client
var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
// put the the following code in the exports.handler in your lambda function
var params = {
TableName: 'Todo-SOMERANDOMSTRING-ENV',//The name of your dynamodb table
Key: {
'id' : 'asasd123-asdsa1-12sdasads-12', // the id of your todo item
},
UpdateExpression: 'set status = :s',
ExpressionAttributeValues: {
':s' : 'To do' // what the new status should be
}
};
// and run the update function
docClient.update(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
编辑
根据您的评论,我认为您可能在 DynamoDB Lambda 触发器部分 (?),您的样板 lambda 函数可能如下所示:
exports.handler = function (event, context) {
console.log(JSON.stringify(event, null, 2));
event.Records.forEach((record) => {
console.log(record.eventID);
console.log(record.eventName);
console.log('DynamoDB Record: %j', record.dynamodb);
});
context.done(null, 'Successfully processed DynamoDB record');
};
我自己之前没有做过这种类型的触发器,所以我不完全确定记录中的数据是如何构造的,但我认为它可能会像这样访问:
record.data.id
//or
record.Item.id
您可以通过转至 lambda 控制台找到并打开您的 lambda 函数,转至“监控”,然后在 AWS 中打开“查看 CloudWatch 中的日志”,并在创建项目后检查 CloudWatch 日志。
假设它是 record.Item.id,您的 lambda 代码可能如下所示(未经测试):
var AWS = require('aws-sdk');
AWS.config.update({region: 'REGION'});
var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
exports.handler = function (event, context) {
console.log(JSON.stringify(event, null, 2));
event.Records.forEach((record) => {
var params = {
TableName: 'YOUR-DB-TABLE-NAME',
Key: {
'id' : record.Item.id,
},
UpdateExpression: 'set status = :status',
ExpressionAttributeValues: {
':status' : 'To do' // what the new status should be
}
};
docClient.update(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
});
context.done(null, 'Successfully processed DynamoDB record');
};
我认为这段代码并不完整,您可能需要更改“context.done”函数的工作方式/时间(因为我认为它会在代码完成更新项目之前运行),但它可能让您朝着正确的方向前进。