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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With