- Newest
- Most votes
- Most comments
My few cents:
- Please Check CloudWatch logs for specific error messages
- If MSK is in a VPC, ensure Lambda is in the same VPC or has VPC connectivity. Basically verify your MSK cluster is accessible from the Lambda function's network location.
- Make sure Lambda execution role has MSK permissions: ex: kafka-cluster:Connect kafka-cluster:AlterCluster kafka-cluster:DescribeCluster kafka-cluster:WriteData kafka-cluster:CreateTopic kafka-cluster:DescribeTopic
- Make sure to close the Producer.
try:
# producer code
finally:
producer.close()
- You're creating the topic and immediately trying to send to it. Add a delay or retry logic
create_topic(bootstrap_servers, topic, token_provider)
time.sleep(2) # Give topic time to be ready
answered a year ago
Hey Manoj, Thanks for the insights!
My Lambda is in the same VPC and my MSK cluster is accessible from lambda since the SGs have been configured correctly - so have the IAM roles :).
Regarding cloudwatch honestly - i am not seeing any errors, the only logs I am seeing are until "Key: ..." right before the producer.send() but i do know producer.send() is working correctly since i put a log after that too and its being executed. The issue is in producer.flush() and not seeing any errors makes it really hard to debug!
I tried the two of your suggestions and honestly I am still not seeing any difference - would really appreciate further help on this topic 🙏
Based on your description, it appears that your Lambda function is hanging during the producer.flush() operation when trying to send messages to your MSK cluster. This is likely due to one of several common issues with Kafka client connectivity.
First, let's address why producer.flush() is blocking execution. This method waits until all outstanding messages have been acknowledged by the Kafka broker. If the broker isn't responding or there's a connectivity issue, this call will block until it times out.
Here are some potential causes and solutions:
-
Client Failover Issues: When a Kafka client can't reach a broker or the broker isn't the leader for a partition, it will try to update its metadata and retry. This typically resolves within 1-3 seconds but can take longer if there are configuration issues. Your current setup with
retries=0means it won't retry failed sends, which could be problematic. -
Authentication Problems: Since you're using IAM authentication with SASL_OAUTHBEARER, verify that your Lambda function's execution role has the necessary permissions to interact with the MSK cluster.
-
Network Configuration: Even though you mentioned the security groups are configured correctly, double-check that the Lambda function can reach the MSK cluster on port 9098. Since your MSK cluster is in a private subnet, ensure that the Lambda function is also in the same VPC and has proper subnet configurations.
-
Client Configuration: Your current configuration has some potentially problematic settings:
retries=0: This disables retries completelymax_block_ms=5000: This is quite low and might cause the producer to give up too quicklyrequest_timeout_ms=10000: This might not be enough time for the request to complete
Try modifying your producer configuration:
producer = KafkaProducer( bootstrap_servers=[bootstrap_servers], security_protocol='SASL_SSL', sasl_mechanism='OAUTHBEARER', sasl_oauth_token_provider=token_provider, acks="1", retries=5, # Allow some retries retry_backoff_ms=100, # Time between retries max_block_ms=60000, # Increase blocking time request_timeout_ms=30000 # Increase request timeout )
Also, consider adding error handling around your send() and flush() operations:
try: future = producer.send(topic, json.dumps({'bucket': bucket_name, 'key': key}).encode('utf-8')) # Get the result with a timeout to avoid blocking indefinitely record_metadata = future.get(timeout=30) print(f"Message sent to partition {record_metadata.partition} at offset {record_metadata.offset}") producer.flush(timeout=30) # Add a timeout to flush print(f"Message successfully flushed to Kafka topic: {topic}") except Exception as e: print(f"Error sending message to Kafka: {e}") finally: producer.close(timeout=5) # Always close the producer
This approach will give you more visibility into what's happening and prevent indefinite blocking.
If you're still experiencing issues, you might want to check if your topic exists and has the correct configuration by examining the output of your create_topic function. Also, consider temporarily increasing the Lambda function timeout to give more time for debugging.
Sources
Broker offline and client failover - Amazon Managed Streaming for Apache Kafka
AWS MSK is not able to load balance records to all the consumers in a consumer group | AWS re:Post
answered a year ago
Relevant content
asked 2 years ago

Based on your debugging and the lack of logs, I think the lambda function is timing out before the kafka request. I see the request_timeout_ms for kafka is 10 seconds. Try increasing the lambda timeout to more than 10 seconds so that any exceptions thrown by the producer can be captured.