Skip to content

hktr92/runo-sqs-lambda-runtime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

runo-sqs-lambda-runtime

A tiny runtime for handling AWS SQS events in AWS Lambda with:

  • Parallel processing: Each SQS record is processed in its own Tokio task
  • Partial batch failure support: Automatically handles PartialBatchFailure responses when individual records fail
  • Robust error handling: Classifies and handles different error types (handler errors, task cancellation, panics)
  • Observability: Propagates message IDs through tracing spans for better logging and debugging

Usage

Add this to your Cargo.toml:

[dependencies]
runo-sqs-lambda-runtime = "0.1.0"
tokio = { version = "1.0", features = ["macros"] }
serde = { version = "1.0", features = ["derive"] }
tracing = "0.1"

Example

use runo_sqs_lambda_runtime::{run_in_lambda, Context, Result};
use serde::Deserialize;

#[derive(Deserialize)]
struct Greeter {
    pub person: String,
}

async fn sqs_handler(payload: Greeter, context: Context) -> Result<()> {
    tracing::info!(
        message_id = %context.id,
        "Hello, {}!",
        payload.person
    );

    // Your business logic here
    // Return Err(error.into()) for failed messages that should be retried

    Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::init();
    run_in_lambda(&sqs_handler).await
}

Features

  • Type-safe message handling: Generic over your message payload type with automatic JSON deserialization
  • Context access: Each handler receives a Context with message ID, headers, and Lambda runtime context
  • Automatic retries: Failed messages are automatically marked for retry via SQS batch failure responses
  • Structured logging: Message IDs are automatically included in tracing spans
  • Panic recovery: Task panics are caught and logged, with the message marked for retry

Context

The Context provides:

  • id: The SQS message ID
  • headers: Typed access to SQS message attributes via get() and get_or()
  • lambda_context: The original AWS Lambda context

Error Handling

Your handler should return:

  • Ok(()) for successfully processed messages
  • Err(error) for messages that should be retried (will be included in batch failure response)

The runtime handles:

  • Deserialization errors (message marked for retry)
  • Handler errors (message marked for retry)
  • Task panics (message marked for retry)
  • Task cancellation (message marked for retry)

License

This project is licensed under the MIT License.

About

A tiny runtime for handling AWS SQS events in AWS Lambda

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages