Skip to content

Commit 213e880

Browse files
authored
Add example of SQS integration (#560)
Signed-off-by: David Calavera <david.calavera@gmail.com> Signed-off-by: David Calavera <david.calavera@gmail.com>
1 parent 9baeb98 commit 213e880

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

examples/basic-lambda/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ serde = "1.0.136"
1616
tokio = { version = "1", features = ["macros"] }
1717
tracing = { version = "0.1", features = ["log"] }
1818
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
19-
20-

examples/basic-sqs/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "basic-sqs"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# Starting in Rust 1.62 you can use `cargo add` to add dependencies
7+
# to your project.
8+
#
9+
# If you're using an older Rust version,
10+
# download cargo-edit(https://github.com/killercup/cargo-edit#installation)
11+
# to install the `add` subcommand.
12+
#
13+
# Running `cargo add DEPENDENCY_NAME` will
14+
# add the latest version of a dependency to the list,
15+
# and it will keep the alphabetic ordering for you.
16+
17+
[dependencies]
18+
aws_lambda_events = "0.7.2"
19+
lambda_runtime = { path = "../../lambda-runtime" }
20+
serde = "1.0.136"
21+
tokio = { version = "1", features = ["macros"] }
22+
tracing = { version = "0.1", features = ["log"] }
23+
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }

examples/basic-sqs/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# AWS Lambda Function that receives events from SQS
2+
3+
This example shows how to process events from a SQS queue.
4+
5+
## Build & Deploy
6+
7+
1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda#installation)
8+
2. Build the function with `cargo lambda build --release`
9+
3. Deploy the function to AWS Lambda with `cargo lambda deploy --iam-role YOUR_ROLE`
10+
11+
## Build for ARM 64
12+
13+
Build the function with `cargo lambda build --release --arm64`

examples/basic-sqs/src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use aws_lambda_events::event::sqs::SqsEventObj;
2+
use lambda_runtime::{run, service_fn, Error, LambdaEvent};
3+
use serde::{Deserialize, Serialize};
4+
5+
/// Object that you send to SQS and plan to process on the function.
6+
#[derive(Deserialize, Serialize)]
7+
struct Data {
8+
id: String,
9+
text: String,
10+
}
11+
12+
/// This is the main body for the function.
13+
/// You can use the data sent into SQS here.
14+
async fn function_handler(event: LambdaEvent<SqsEventObj<Data>>) -> Result<(), Error> {
15+
let data = &event.payload.records[0].body;
16+
tracing::info!(id = ?data.id, text = ?data.text, "data received from SQS");
17+
18+
Ok(())
19+
}
20+
21+
#[tokio::main]
22+
async fn main() -> Result<(), Error> {
23+
tracing_subscriber::fmt()
24+
.with_max_level(tracing::Level::INFO)
25+
// disable printing the name of the module in every log line.
26+
.with_target(false)
27+
// disabling time is handy because CloudWatch will add the ingestion time.
28+
.without_time()
29+
.init();
30+
31+
run(service_fn(function_handler)).await
32+
}

examples/check-examples.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env bash
22
set -e
33

4+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
5+
export CARGO_TARGET_DIR="$SCRIPT_DIR/../target"
6+
7+
echo "==> Using shared target directory: $CARGO_TARGET_DIR"
8+
49
for f in *; do
510
if [ -d "$f" ]; then
611
echo "==> Checking example: $f"

0 commit comments

Comments
 (0)