1+ use aws_lambda_events:: event:: kafka:: KafkaEvent ;
2+ use lambda_runtime:: { run, service_fn, tracing, Error , LambdaEvent } ;
3+ use base64:: prelude:: * ;
4+ use serde_json:: { Value } ;
5+ use tracing:: { info} ;
6+
7+ /// Pre-Requisites:
8+ /// 1. Install Cargo Lambda - see https://www.cargo-lambda.info/guide/getting-started.html
9+ /// 2. Add packages tracing, tracing-subscriber, serde_json, base64
10+ ///
11+ /// This is the main body for the function.
12+ /// Write your code inside it.
13+ /// There are some code example in the following URLs:
14+ /// - https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples
15+ /// - https://github.com/aws-samples/serverless-rust-demo/
16+
17+ async fn function_handler ( event : LambdaEvent < KafkaEvent > ) -> Result < Value , Error > {
18+
19+ let payload = event. payload . records ;
20+
21+ for ( _name, records) in payload. iter ( ) {
22+
23+ for record in records {
24+
25+ let record_text = record. value . as_ref ( ) . ok_or ( "Value is None" ) ?;
26+ info ! ( "Record: {}" , & record_text) ;
27+
28+ // perform Base64 decoding
29+ let record_bytes = BASE64_STANDARD . decode ( record_text) ?;
30+ let message = std:: str:: from_utf8 ( & record_bytes) ?;
31+
32+ info ! ( "Message: {}" , message) ;
33+ }
34+
35+ }
36+
37+ Ok ( ( ) . into ( ) )
38+ }
39+
40+ #[ tokio:: main]
41+ async fn main ( ) -> Result < ( ) , Error > {
42+
43+ // required to enable CloudWatch error logging by the runtime
44+ tracing:: init_default_subscriber ( ) ;
45+ info ! ( "Setup CW subscriber!" ) ;
46+
47+ run ( service_fn ( function_handler) ) . await
48+ }
0 commit comments