2

I am trying to create a Lambda in AWS that serves as a producer to an MSK topic. All the AWS docs say to create a new EC2 instance, but as my Lambda is in the same VPC I feel like this should work. I am very new to this and I notice my log statement never hits in my producer.on function. I am using nodejs and the kafka-node module. The code can be found below.

Essentially, I am just wondering if anyone knows how to do this and why the producer.on function is never hit when I run test through the Lambda? This is just some test code to see if I can get it to send, but if any more data is needed oy help please let me know and thanks in advance.

exports.handler = async (event, context,callback) => {

const kafka = require('kafka-node');
const bp = require('body-parser');

const kafka_topic = 'MyTopic';
const Producer = kafka.Producer;
var KeyedMessage = kafka.KeyedMessage;
const Client = kafka.Client;
const client = new kafka.KafkaClient({kafkaHost: 'myhost:9094'});

console.log('client :: '+JSON.stringify(client));

const producer = new Producer(client);
console.log('about to hit producer code');

producer.on('ready', function() {
  console.log('Hello there!')
  let message = 'my message';
  let keyedMessage = new KeyedMessage('keyed', 'me keyed message');
  
    producer.send([
    { topic: kafka_topic, partition: 0, messages: [message, keyedMessage], attributes: 0 }
  ], function (err, result) {
    console.log(err || result);
    process.exit();
  });
});

producer.on('error', function (err) {
  console.log('error', err);
});
}
return "success";
paulsm4
  • 114,292
  • 17
  • 138
  • 190
drumurr
  • 33
  • 1
  • 4
  • OK: You've got [Amazon Managed Streaming for Apache Kafka (Amazon MSK)](https://aws.amazon.com/msk/) set up, you want to create a Kafka [producer](https://kafka.js.org/docs/producer-example) in Javascript ... and you'd like to implement the producer with an AWS lambda, instead of a full-fledged AWS EC2 VM. Correct? Q: Where exactly is the above NodeJS code deployed? Your AWS lambda, correct? Q: Exactly how are you trying to invoke it? – paulsm4 Nov 07 '20 at 23:06
  • All of your assumptions are indeed correct. The Lambda is in a Kinesis Data Firehose and is invoked as data streams in. So it should hit the lambda and send that data to MSK. Does that make sense? – drumurr Nov 07 '20 at 23:37
  • Thank you for your clarification :) Yes, you should be able to implement a NodeJS/Kafka producer with an AWS lambda. But no, I'm not familiar with "Kinesis Data Firehose". So I don't know why your lambda isn't getting invoked. Sorry I can't be of more help... – paulsm4 Nov 07 '20 at 23:50
  • Hey no worries and thanks for checking in. The clarification might help someone help me figure it out. Really appreciate you taking the time to help. – drumurr Nov 07 '20 at 23:59
  • AFAIK Kinesis Data Streams can automatically deliver streams to S3, Redshift, Elasticsearch, Datadog, MongoDB, New Relic, Splunk, and HTTP endpoints. Not MSK directly. So I presume you're writing this function as an HTTP endpoint? If so, you may want to double check the requirements for writing a HTTP endpoint that acts as a sink for Kinesis Data Streams: https://docs.aws.amazon.com/firehose/latest/dev/httpdeliveryrequestresponse.html – Ricardo Ferreira Nov 09 '20 at 17:53
  • @drumurr any updates on this? Were you able to solve the issue? – Olalekan Sogunle Nov 27 '20 at 10:05
  • This was solved as it was a permissions issue that I didn't have access to change. Not sure if that's something I should put as an answer, but once the permission was added it worked fine. – drumurr Nov 30 '20 at 15:57

1 Answers1

0

What you need is to be able to produce messages on your MSK cluster using REST API. Why not setup a REST proxy for MSK as detailed here and then call this API to produce your messages to MSK.

Olalekan Sogunle
  • 2,299
  • 1
  • 20
  • 26