Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62f54f1

Browse files
authored
Merge pull request #210 from sachinh/sachinh-feature-integration-msk-to-lambda-rust
Added Rust implementation for msk-to-lambda integration
2 parents c806564 + f1a38ad commit 62f54f1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

integration-msk-to-lambda/main.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)