From 2461904a2821c2f69c4333cf6824344a2a2070f8 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Sun, 30 Oct 2022 19:29:48 -0700 Subject: [PATCH] Add example of SQS integration Signed-off-by: David Calavera --- examples/basic-lambda/Cargo.toml | 2 -- examples/basic-sqs/Cargo.toml | 23 +++++++++++++++++++++++ examples/basic-sqs/README.md | 13 +++++++++++++ examples/basic-sqs/src/main.rs | 32 ++++++++++++++++++++++++++++++++ examples/check-examples.sh | 5 +++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 examples/basic-sqs/Cargo.toml create mode 100644 examples/basic-sqs/README.md create mode 100644 examples/basic-sqs/src/main.rs diff --git a/examples/basic-lambda/Cargo.toml b/examples/basic-lambda/Cargo.toml index ebf36913..e9e8d635 100644 --- a/examples/basic-lambda/Cargo.toml +++ b/examples/basic-lambda/Cargo.toml @@ -16,5 +16,3 @@ serde = "1.0.136" tokio = { version = "1", features = ["macros"] } tracing = { version = "0.1", features = ["log"] } tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] } - - diff --git a/examples/basic-sqs/Cargo.toml b/examples/basic-sqs/Cargo.toml new file mode 100644 index 00000000..55da60e0 --- /dev/null +++ b/examples/basic-sqs/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "basic-sqs" +version = "0.1.0" +edition = "2021" + +# Starting in Rust 1.62 you can use `cargo add` to add dependencies +# to your project. +# +# If you're using an older Rust version, +# download cargo-edit(https://github.com/killercup/cargo-edit#installation) +# to install the `add` subcommand. +# +# Running `cargo add DEPENDENCY_NAME` will +# add the latest version of a dependency to the list, +# and it will keep the alphabetic ordering for you. + +[dependencies] +aws_lambda_events = "0.7.2" +lambda_runtime = { path = "../../lambda-runtime" } +serde = "1.0.136" +tokio = { version = "1", features = ["macros"] } +tracing = { version = "0.1", features = ["log"] } +tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] } diff --git a/examples/basic-sqs/README.md b/examples/basic-sqs/README.md new file mode 100644 index 00000000..16731de1 --- /dev/null +++ b/examples/basic-sqs/README.md @@ -0,0 +1,13 @@ +# AWS Lambda Function that receives events from SQS + +This example shows how to process events from a SQS queue. + +## Build & Deploy + +1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda#installation) +2. Build the function with `cargo lambda build --release` +3. Deploy the function to AWS Lambda with `cargo lambda deploy --iam-role YOUR_ROLE` + +## Build for ARM 64 + +Build the function with `cargo lambda build --release --arm64` \ No newline at end of file diff --git a/examples/basic-sqs/src/main.rs b/examples/basic-sqs/src/main.rs new file mode 100644 index 00000000..24fbd573 --- /dev/null +++ b/examples/basic-sqs/src/main.rs @@ -0,0 +1,32 @@ +use aws_lambda_events::event::sqs::SqsEventObj; +use lambda_runtime::{run, service_fn, Error, LambdaEvent}; +use serde::{Deserialize, Serialize}; + +/// Object that you send to SQS and plan to process on the function. +#[derive(Deserialize, Serialize)] +struct Data { + id: String, + text: String, +} + +/// This is the main body for the function. +/// You can use the data sent into SQS here. +async fn function_handler(event: LambdaEvent>) -> Result<(), Error> { + let data = &event.payload.records[0].body; + tracing::info!(id = ?data.id, text = ?data.text, "data received from SQS"); + + 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 +} diff --git a/examples/check-examples.sh b/examples/check-examples.sh index 28824579..f55bced9 100755 --- a/examples/check-examples.sh +++ b/examples/check-examples.sh @@ -1,6 +1,11 @@ #!/usr/bin/env bash set -e +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +export CARGO_TARGET_DIR="$SCRIPT_DIR/../target" + +echo "==> Using shared target directory: $CARGO_TARGET_DIR" + for f in *; do if [ -d "$f" ]; then echo "==> Checking example: $f"