1

I am trying out on ballerina and want to know if we can have a ballerina program which is a kafka consumer can be exposed as a AWS Lambda function.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
  • As per my answer its not possible. Can you bit explain the what exact you case you want to implement? Then we can figure out an alternative way to implement. – Tharik Kanaka May 03 '19 at 09:15

2 Answers2

2

AWS Lambda functions needs to be triggered from somewhere (Not working as listeners). In that case you wont be able expose Ballerina Kafka consumer with Amazon AWS.

Tharik Kanaka
  • 2,490
  • 6
  • 31
  • 54
1

You cannot create a Kafka Listener as an AWS lambda, but you can create a non-listener type Kafka consumer as an AWS lambda function.

You can write ballerina function to handle Kafka SimpleConsumer object's functions (Current Ballerina-By-Guide for Kafka is treating Kafka consumer as a service listener), and use that function to create AWS lambdas.

WSO2-Kafka connector offers various functionalities to handle Kafka consumer manually. Read the documentation for more info, it is available on WSO2-Kafka release.

You can generate AWS lambdas by adding AWS lambda annotation on top of the function.

@awslambda:Function
function kafkaConsumerService(awslambda:Context ctx, json inputs) returns json|error {

  kafka:ConsumerConfig consumerConfig = {
    // Consumer configs
  };

  kafka:SimpleConsumer kafkaConsumer = new(consumerConfig);

  // You can implement Kafka consume functionalities here.
  // For an example, you can poll consumer using kafkaConsumer->poll(<duration>);
  var results = kafkaConsumer->poll(1000);

  if (results is error) {
    // Handle error
  } else {
    // Handle records received
  }
}

Then you can generate AWS Lambdas by following the Ballerina documentation on AWS Lambdas.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
  • I tried an echo Lambda function by importing wso2/kafka, but Lambda function was unable to resolve it while testing, it threw an error saying "native function not available wso2/kafka:0.0.0:SimpleConsumer.registerListener". Can you please let me know what I should do here? – Prabakaran Thodithot Sembiyan May 03 '19 at 10:49
  • @PrabakaranThodithotSembiyan Seems like you have two Kafka connector versions reside in the same Ballerina distribution. Check inside `/bre/lib/` directory whether you have two or more `wso2-kafka-*.jar` files. Remove them, and re-install `Ballerina-Kafka` connector. (Or simply you can remove only the previous version jars, and keep the most recent one and continue) – ThisaruG May 03 '19 at 10:51
  • I exposed my ballerina function as Lambda and while calling the lambda function, I got the above-mentioned error. Also, I have only one version of wso2-kafka-*.jar in bre/lib. – Prabakaran Thodithot Sembiyan May 03 '19 at 12:04
  • @PrabakaranThodithotSembiyan You need to put Kafka Connector jars (`wso2-kafka-*.jar`) inside the lambda zip file that's generated, inside a "lib" directory in the root. You need to create this directory inside the zip file. Otherwise the connector functions will not work. – ThisaruG May 03 '19 at 12:38
  • Hi Thisaru, I am facing the below error while I used the code you shared (with my kafka consumer config). "client object declaration not allowed here, declare at the top of a function or at module level" - at "kafka:SimpleConsumer kafkaConsumer = new(consumerConfig);" Could you please help to resolve this? – Prabakaran Thodithot Sembiyan May 03 '19 at 14:08
  • Which version of ballerina are you using ? – ThisaruG May 03 '19 at 14:16
  • I'm using ballerina-0.990.3 version. – Prabakaran Thodithot Sembiyan May 03 '19 at 14:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192784/discussion-between-thisaru-guruge-and-prabakaran-thodithot-sembiyan). – ThisaruG May 03 '19 at 14:25