Skip to content
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

Add example of SQS integration #560

Merged
merged 1 commit into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions examples/basic-lambda/Cargo.toml
Expand Up @@ -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"] }


23 changes: 23 additions & 0 deletions 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"] }
13 changes: 13 additions & 0 deletions 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`
32 changes: 32 additions & 0 deletions 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<SqsEventObj<Data>>) -> 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
}
5 changes: 5 additions & 0 deletions 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"
Expand Down