前言:我在整個集成過程中,存在最大的問題有兩個,
1. 沒有考慮到lambda函數(shù)的權(quán)限,即對DynamoDB或者其他如Kinesis的權(quán)限授權(quán),導(dǎo)致無法寫入或者讀取。
2.最初使用了異步方式調(diào)用,導(dǎo)致無法寫數(shù)據(jù)到DynamoDB,把代碼改成同步調(diào)用即可
創(chuàng)建Lambda函數(shù)
'use strict'
console.log('Loading function');
var aws = require('aws-sdk');
var docClient = new aws.DynamoDB.DocumentClient({region: 'ap-southeast-1'});
exports.handler = function(event, context,callback) {
var records = event.Records;
if(records){
records.forEach(function(record){
//console.log('record:',record);
//var payload = new Buffer(record.kinesis.data,'base64').toString('ascii');
var payload = Buffer.from(record.kinesis.data, 'base64').toString('ascii');
console.log('Decode payload:',payload);
var params ={
Item:{
date:Date.now(),
message:payload
},
TableName:"Lambda-Dynamo-Write-Read-China"
};
if(docClient){
console.log('docClient is not null');
console.log('params date:',params.Item.date);
// //await ddb.put(params).promise();
//await ddb.put(params).promise();
docClient.put(params,function(err,data){
if(err){
console.log("Fail to Write into AWS DynamoDB");
callback(err,null);
}else{
console.log("Successfully write item into AWS DynamoDB")
callback(null,data)
}
});
}else{
console.log('docClient is null');
}
});
}else{
console.log('records is empty');
}
};
?創(chuàng)建好之后保存,然后進行部署(deploy)
然后在IAM中,對相應(yīng)的角色進行授權(quán)(lambda-kinsis-guangzhou-role-xxx)這個角色名是創(chuàng)建lambda自動生成的,在上面增加相應(yīng)的權(quán)限即可。
創(chuàng)建觸發(fā)器
?創(chuàng)建DynamoDB 的表
?創(chuàng)建好后,給表增加屬性字段
?注意:只需要增加一個分區(qū)字段date即可,message字段,在Lambda方法中,作為入?yún)魅爰纯?/p>
創(chuàng)建Kinesis
?
在本地使用aws-cli發(fā)送消息報文
?在DynamoDB如果能看到發(fā)送的數(shù)據(jù),即集成成功文章來源:http://www.zghlxwxcb.cn/news/detail-642891.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-642891.html
到了這里,關(guān)于AWS中l(wèi)ambda與DynamoDB的集成的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!