amazon web services - Pushing AWS Lambda data to Kinesis Stream -
is there way push data lambda function kinesis stream? have searched internet have not found examples related it.
thanks.
yes, can send information lambda kinesis stream , simple do. make sure running lambda right permissions.
- create file called kinesis.js, file provide 'save' function receives payload , sends kinesis stream. want able include 'save' function anywhere want send data stream. code:
const aws = require('aws-sdk'); const kinesisconstant = require('./kinesisconstants'); //keep 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 save strings 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 able take new writes (active or updating good) if(data.streamdescription.streamstatus === kinesisconstant.state.active || data.streamdescription.streamstatus === kinesisconstant.state.updating ) { savepayload(payload); } else { console.log(`kinesis stream ${kinesisconstant.stream_name} ${data.streamdescription.streamstatus}.`); console.log(`record lost`, json.parse(payload)); } } }); };
- create kinesisconstant.js file keep consistent :)
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' }
- your handler file: added 'done' function send response whoever wants send data stream 'kinesis.save(event)' work.
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 send stream done(null, event); }
Comments
Post a Comment