-
Notifications
You must be signed in to change notification settings - Fork 98
Rust Snippets for Lambda-SQS, Lambda-Kinesis, and Lambda-Kinesis with Batch Failures #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e59b81b
Sample Amazon SQS function code using Rust without batch item handling.
kazhura-aws 8bffa42
Consuming Kinesis event with Lambda using Rust without batch item han…
kazhura-aws b5fb89e
Consuming Kinesis event with Lambda using Rust with batch item handling.
kazhura-aws b559e05
Merge branch 'main' into main
kazhura-aws d4c0f9d
Merge branch 'aws-samples:main' into main
kazhura-aws 8b44853
code review related fixes
kazhura-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
integration-kinesis-to-lambda-with-batch-item-handling/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use aws_lambda_events::{ | ||
event::kinesis::KinesisEvent, | ||
kinesis::KinesisEventRecord, | ||
streams::{KinesisBatchItemFailure, KinesisEventResponse}, | ||
}; | ||
use lambda_runtime::{run, service_fn, Error, LambdaEvent}; | ||
|
||
async fn function_handler(event: LambdaEvent<KinesisEvent>) -> Result<KinesisEventResponse, Error> { | ||
let mut response = KinesisEventResponse { | ||
batch_item_failures: vec![], | ||
}; | ||
|
||
if event.payload.records.is_empty() { | ||
tracing::info!("No records found. Exiting."); | ||
return Ok(response); | ||
} | ||
|
||
for record in &event.payload.records { | ||
tracing::info!( | ||
"EventId: {}", | ||
record.event_id.as_deref().unwrap_or_default() | ||
); | ||
|
||
let record_processing_result = process_record(record); | ||
|
||
if record_processing_result.is_err() { | ||
response.batch_item_failures.push(KinesisBatchItemFailure { | ||
item_identifier: record.kinesis.sequence_number.clone(), | ||
}); | ||
/* Since we are working with streams, we can return the failed item immediately. | ||
Lambda will immediately begin to retry processing from this failed item onwards. */ | ||
return Ok(response); | ||
} | ||
} | ||
|
||
tracing::info!( | ||
"Successfully processed {} records", | ||
event.payload.records.len() | ||
); | ||
|
||
Ok(response) | ||
} | ||
|
||
fn process_record(record: &KinesisEventRecord) -> Result<(), Error> { | ||
let record_data = std::str::from_utf8(record.kinesis.data.as_slice()); | ||
|
||
if let Some(err) = record_data.err() { | ||
tracing::error!("Error: {}", err); | ||
return Err(Error::from(err)); | ||
} | ||
|
||
let record_data = record_data.unwrap_or_default(); | ||
|
||
// do something interesting with the data | ||
tracing::info!("Data: {}", record_data); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
// disable printing the name of the module in every log line. | ||
.with_target(false) | ||
// disabling time is handy because CloudWatch will add the ingestion time. | ||
.without_time() | ||
.init(); | ||
|
||
run(service_fn(function_handler)).await | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use aws_lambda_events::event::kinesis::KinesisEvent; | ||
use lambda_runtime::{run, service_fn, Error, LambdaEvent}; | ||
|
||
async fn function_handler(event: LambdaEvent<KinesisEvent>) -> Result<(), Error> { | ||
if event.payload.records.is_empty() { | ||
tracing::info!("No records found. Exiting."); | ||
return Ok(()); | ||
} | ||
|
||
event.payload.records.iter().for_each(|record| { | ||
tracing::info!("EventId: {}",record.event_id.as_deref().unwrap_or_default()); | ||
|
||
let record_data = std::str::from_utf8(&record.kinesis.data); | ||
|
||
match record_data { | ||
Ok(data) => { | ||
// log the record data | ||
tracing::info!("Data: {}", data); | ||
} | ||
Err(e) => { | ||
tracing::error!("Error: {}", e); | ||
} | ||
} | ||
}); | ||
|
||
tracing::info!( | ||
"Successfully processed {} records", | ||
event.payload.records.len() | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
// disable printing the name of the module in every log line. | ||
.with_target(false) | ||
// disabling time is handy because CloudWatch will add the ingestion time. | ||
.without_time() | ||
.init(); | ||
|
||
run(service_fn(function_handler)).await | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use aws_lambda_events::event::sqs::SqsEvent; | ||
use lambda_runtime::{run, service_fn, Error, LambdaEvent}; | ||
|
||
async fn function_handler(event: LambdaEvent<SqsEvent>) -> Result<(), Error> { | ||
event.payload.records.iter().for_each(|record| { | ||
// process the record | ||
tracing::info!("Message body: {}", record.body.as_deref().unwrap_or_default()) | ||
}); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
// disable printing the name of the module in every log line. | ||
.with_target(false) | ||
// disabling time is handy because CloudWatch will add the ingestion time. | ||
.without_time() | ||
.init(); | ||
|
||
run(service_fn(function_handler)).await | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.