是否有办法将数据从Lambda函数推送到Kinesis流?我在互联网上搜索过但没有找到任何与之相关的例子。
感谢。
答案 0 :(得分:8)
是的,您可以将信息从Lambda发送到Kinesis Stream,这很简单。确保您使用正确的权限运行Lambda。
const AWS = require('aws-sdk');
const kinesisConstant = require('./kinesisConstants'); //Keep it consistent
const kinesis = new AWS.Kinesis({
apiVersion: kinesisConstant.API_VERSION, //optional
//accessKeyId: '<you-can-use-this-to-run-it-locally>', //optional
//secretAccessKey: '<you-can-use-this-to-run-it-locally>', //optional
region: kinesisConstant.REGION
});
const savePayload = (payload) => {
//We can only save strings into the streams
if( typeof payload !== kinesisConstant.PAYLOAD_TYPE) {
try {
payload = JSON.stringify(payload);
} catch (e) {
console.log(e);
}
}
let params = {
Data: payload,
PartitionKey: kinesisConstant.PARTITION_KEY,
StreamName: kinesisConstant.STREAM_NAME
};
kinesis.putRecord(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log('Record added:',data);
});
};
exports.save = (payload) => {
const params = {
StreamName: kinesisConstant.STREAM_NAME,
};
kinesis.describeStream(params, function(err, data) {
if (err) console.log(err, err.stack);
else {
//Make sure stream is able to take new writes (ACTIVE or UPDATING are good)
if(data.StreamDescription.StreamStatus === kinesisConstant.STATE.ACTIVE
|| data.StreamDescription.StreamStatus === kinesisConstant.STATE.UPDATING ) {
savePayload(payload);
} else {
console.log(`Kinesis stream ${kinesisConstant.STREAM_NAME} is ${data.StreamDescription.StreamStatus}.`);
console.log(`Record Lost`, JSON.parse(payload));
}
}
});
};
module.exports = {
STATE: {
ACTIVE: 'ACTIVE',
UPDATING: 'UPDATING',
CREATING: 'CREATING',
DELETING: 'DELETING'
},
STREAM_NAME: '<your-stream-name>',
PARTITION_KEY: '<string-value-if-one-shard-anything-will-do',
PAYLOAD_TYPE: 'String',
REGION: '<the-region-where-you-have-lambda-and-kinesis>',
API_VERSION: '2013-12-02'
}
const kinesis = require('./kinesis');
exports.handler = (event, context, callback) => {
console.log('LOADING handler');
const done = (err, res) => callback(null, {
statusCode: err ? '400' : '200',
body: err || res,
headers: {
'Content-Type': 'application/json',
},
});
kinesis.save(event); // here we send it to the stream
done(null, event);
}
答案 1 :(得分:3)
这应该像在您的计算机上一样完成。
以下是nodejs
中的示例:
let aws = require('aws');
let kinesis = new aws.Kinesis();
// data that you'd like to send
let data_object = { "some": "properties" };
let data = JSON.stringify(data_object);
// push data to kinesis
const params = {
Data: data,
PartitionKey: "1",
StreamName: "stream name"
}
kinesis.putRecord(params, (err, data) => {
if (err) console.error(err);
else console.log("data sent");
}
请注意,这段代码不会有效,因为Lambda
对您的信息流没有任何权限。
通过AWS
访问Lambda
资源时,最好使用IAM
个角色;
Lambda
时,您可以选择现有/创建角色。IAM
,然后转到角色,然后选择您分配给Lambda
功能的角色名称。putRecord
,putRecords
)。然后,测试Lambda
。
答案 2 :(得分:0)
是的,这可以完成,我试图完成同样的事情,并且能够使用Node.js 4.3运行时在Lambda中完成,并且它也适用于版本6.10。
以下是代码:
在Lambda函数的顶部声明以下内容:
var AWS = require("aws-sdk");
var kinesis = new AWS.Kinesis();
function writeKinesis(rawdata){
data = JSON.stringify(rawdata);
params = {Data: data, PartitionKey: "<PARTITION_KEY>", StreamName: "<STREAM_NAME>"};
kinesis.putRecord(params, (err, data) => {
if (err) console.error(err);
else console.log("data sent");
});
}
现在,在exports.handler中,调用函数:
writeKinesis(<YOUR_DATA>);
要注意的一些事项......对于Kinesis来摄取数据,必须对其进行编码。在下面的示例中,我有一个从CloudWatch获取日志的函数,并将它们发送到Kinesis流。
请注意,我将buffer.toString(&#39; utf8&#39;)的内容插入到writeKinesis函数中:
exports.handler = function(input, context) {
...
var zippedInput = new Buffer(input.awslogs.data, 'base64');
zlib.gunzip(zippedInput, function(error, buffer) {
...
writeKinesis(buffer.toString('utf8'));
...
}
...
}
最后,在IAM中,配置适当的权限。您的Lambda函数必须在包含以下权限的IAM角色的上下文中运行。在我的例子中,我刚刚修改了默认的lambda_elasticsearch_execution角色,以包含一个名为&#34; lambda_kinesis_execution&#34;的策略。使用以下代码:
"Effect": "Allow",
"Action": [
"kinesis:*"
],
"Resource": [
"<YOUR_STREAM_ARN>"
]