- 在DynamoDB当作创建一个新表,然后在项目当中创建,把string value的值填写一下。
- 在IAM创建角色附加 AWSLambdaDynamoDBExecutionRole权限
- 在lambda中创建函数,选择DynamoDB不附加其他条件的蓝图
- 选择第1步创建的DynamoDB表,完成lambda函数的创建。
- 打开lambda函数的index.js,代码进行重写,内容如下
var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB({
region: 'ap-northeast-1'
});
exports.handler = function(event, context) {
var params = {
"TableName": "sbrtest", #表名
"KeyConditionExpression":"this = :sbrtest", #建表时候的关键字和函数名
"ExpressionAttributeValues": {
":sbrtest" : {"S": event.sbrtest} #函数名
}
};
console.log("event:", event);
dynamo.query(params, function(err, data) {
console.log("dynamo_data:", data);
console.log("dynamo_err:", err);
context.done(null, data);
});
};
- 内容进行保存
- 对编辑好的表进行测试,测试代码如下
{
"Records": [
{
"eventID": "1",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"NewImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES",
"SequenceNumber": "111",
"SizeBytes": 26
},
"awsRegion": "ap-northeast-1",
"eventName": "INSERT",
"eventSourceARN": "arn:aws:dynamodb:ap-northeast-1:382459439888:table/sbrtest0313",
"eventSource": "aws:dynamodb"
},
{
"eventID": "2",
"eventVersion": "1.0",
"dynamodb": {
"OldImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"SequenceNumber": "222",
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 59,
"NewImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"awsRegion": "ap-northeast-1",
"eventName": "MODIFY",
"eventSourceARN": "arn:aws:dynamodb:ap-northeast-1:382459439888:table/sbrtest0313",
"eventSource": "aws:dynamodb"
},
{
"eventID": "3",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 38,
"SequenceNumber": "333",
"OldImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"awsRegion": "ap-northeast-1",
"eventName": "REMOVE",
"eventSourceARN": "arn:aws:dynamodb:ap-northeast-1:382459439888:table/sbrtest0313",
"eventSource": "aws:dynamodb"
}
]
}
- 函数页面会显示测试成功
- 在DynamoDB的触发选项中,也能够发现出现了新的数据。