Asish
Asish

Reputation: 409

Push data from AWS lambda to Kinesis Firehose

I have an apiGateway endpoint and I am sending some post request to the endpoint. The integration type for the apigateway is lambda function. I want the lambda function to listen to the post data coming on the apigateway and push those data to kinesis firehose.

Can anyone help me get a sample node js lambda code that will push the incoming data to kinesis firehose. I tried to search for this but could not get anything.

Thanks

Upvotes: 5

Views: 12725

Answers (2)

Hiro
Hiro

Reputation: 578

I encountered Cannot read properties of undefined (reading 'byteLength') error using decodeURIComponent on Lambda NodeJS 18.x. Here is my working example using AWS SDK for JavaScript v3 with ES Modules

import {
  FirehoseClient,
  PutRecordCommand,
} from '@aws-sdk/client-firehose';

const streamName = '<KINESIS_FIREHOSE_NAME>';
const client = new FirehoseClient();

export const handler = async (event) => {
  try {
    const input = {
      DeliveryStreamName: streamName,
      Record: {
        Data: Buffer.from(JSON.stringify(event)),
      },
    };
    const command = new PutRecordCommand(input);
    const response = await client.send(command);
    console.log('success %o', response);
  } catch (err) {
    console.error('error %s', err.message);
  }
};

Upvotes: 1

Asish
Asish

Reputation: 409

I got it.

This is a sample code :

var AWS = require('aws-sdk');
var firehose = new AWS.Firehose();

exports.handler = function(event, context) {
    var params = {
        DeliveryStreamName: <STRING>,
        Record: { 
            Data: decodeURIComponent(event)
        }
    };
    firehose.putRecord(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response

        context.done();
    });
};

Upvotes: 17

Related Questions