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.

  1. 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));        }      }    });  };

  1. 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'  }

  1. 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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -