Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Messages from AWS Lambda to Kafka

I have a kafka machine running in AWS which consists of several topics. I have the following Lambda function which Produces a message and push that to one of the kafka topic.

import json from kafka
import KafkaClient from kafka
import SimpleProducer from kafka
import KafkaProducer

def lambda_handler(event, context):
    kafka = KafkaClient("XXXX.XXX.XX.XX:XXXX")
    print(kafka)
    producer = SimpleProducer(kafka, async = True)
    print(producer)
    task_op = {
        "'message": "Hai, Calling from AWS Lambda"
    }
    print(json.dumps(task_op))
    producer.send_messages("topic_atx_ticket_update",json.dumps(task_op).encode('utf-8'))
    print(producer.send_messages)
    return ("Messages Sent to Kafka Topic")

But I see messages are not pushed as i expected.

Note: No Issues in Roles and Policies, Connectivity.

like image 344
RajNikhil Marpu Avatar asked Jul 19 '19 05:07

RajNikhil Marpu


People also ask

How do I use upstash Kafka with AWS Lambda?

We will use Upstash Kafka as a source for an AWS Lambda function. The produced messages will trigger AWS Lambda, so your Lambda function will process the messages. Because Upstash Kafka is a true serverless product, the whole pipeline will be serverless.

How do I trigger a lambda function from a Kafka topic?

Let’s say we have a Kafka topic and an AWS Lambda function. To trigger the Lambda function, we need something which translates a Kafka message into a payload the function can understand. To do this translation we can use a Kafka AWS Lambda Sink Connector.

How do I create a lambda function in AWS Lambda?

Create a Lambda function that uses the self-hosted cluster and topic as an event source: From the Lambda console, select Create function. Enter a function name, and select Node.js 12.x as the runtime. Select the Permissions tab, and select the role name in the Execution role panel to open the IAM console.

How do I use Kafka as an event source?

Using Kafka as an event source operates in a similar way to using Amazon SQS or Amazon Kinesis. In all cases, the Lambda service internally polls for new records or messages from the event source, and then synchronously invokes the target Lambda function.


1 Answers

While Creating a Kafka Producer object,

producer = SimpleProducer(kafka, async=True)

"async" String should be False, like

producer = SimpleProducer(kafka, async=False)

Then,

you can send the Kafka Message to a topic from AWS Lambda.

like image 145
RajNikhil Marpu Avatar answered Oct 17 '22 16:10

RajNikhil Marpu