Skip to content

Commit 8510fea

Browse files
committed
add multi functions example
1 parent c565173 commit 8510fea

File tree

8 files changed

+165
-0
lines changed

8 files changed

+165
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[workspace]
2+
3+
members = [
4+
"producer",
5+
"consumer",
6+
"pizza_lib",
7+
]
8+
9+
[profile.release]
10+
opt-level = 'z'
11+
lto = true
12+
codegen-units = 1
13+
panic = 'abort'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# AWS Lambda Function example
2+
3+
## Build & Deploy
4+
5+
1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda#installation)
6+
2. Build the function with `cargo lambda build --release`
7+
3. Deploy the function to AWS Lambda with `cargo lambda deploy --iam-role YOUR_ROLE`
8+
9+
## Build for ARM 64
10+
11+
Build the function with `cargo lambda build --release --arm64`
12+
13+
## Add the SQS trigger to the consumer function
14+
15+
You can use aws-cli to create an event source mapping:
16+
17+
`aws lambda create-event-source-mapping \
18+
--function-name consumer \
19+
--region <region> \
20+
--event-source-arn <your-SQS-queue-ARN> \
21+
--batch-size 1`
22+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "consumer"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
7+
[dependencies]
8+
#tracing
9+
tracing = "0.1.40"
10+
tracing-subscriber = "0.3.17"
11+
12+
#aws depdencies
13+
aws-sdk-config = "0.35.0"
14+
aws-sdk-sqs = "0.35.0"
15+
aws_lambda_events = { version = "0.11.1", features = ["sqs"], default-features = false }
16+
17+
#lambda runtime
18+
lambda_runtime = "0.8.1"
19+
tokio = { version = "1", features = ["macros"] }
20+
21+
#shared lib
22+
pizza_lib = { path = "../pizza_lib" }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use aws_lambda_events::event::sqs::SqsEventObj;
2+
use lambda_runtime::{service_fn, Error, LambdaEvent};
3+
use pizza_lib::Pizza;
4+
5+
#[tokio::main]
6+
async fn main() -> Result<(), Error> {
7+
tracing_subscriber::fmt()
8+
.with_max_level(tracing::Level::INFO)
9+
.with_target(false)
10+
.with_ansi(false)
11+
.without_time()
12+
.init();
13+
let func = service_fn(func);
14+
lambda_runtime::run(func).await?;
15+
Ok(())
16+
}
17+
18+
async fn func(event: LambdaEvent<SqsEventObj<Pizza>>) -> Result<(), Error> {
19+
for record in event.payload.records.iter() {
20+
let pizza = &record.body;
21+
println!("Pizza name: {} with toppings: {:?}", pizza.name, pizza.toppings);
22+
}
23+
Ok(())
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "pizza_lib"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
serde = { version = "1.0.191", features = ["derive"] }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Serialize, Deserialize)]
4+
pub struct Pizza {
5+
pub name: String,
6+
pub toppings: Vec<String>,
7+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "producer"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[package.metadata.lambda.deploy]
7+
env = { "QUEUE_URL" = "https://changeMe" }
8+
9+
[dependencies]
10+
#tracing
11+
tracing = "0.1.40"
12+
tracing-subscriber = "0.3.17"
13+
14+
#aws dependencies
15+
aws-config = "0.57.1"
16+
aws-sdk-config = "0.35.0"
17+
aws-sdk-sqs = "0.35.0"
18+
19+
#lambda runtime
20+
lambda_runtime = "0.8.1"
21+
serde_json = "1.0.108"
22+
tokio = { version = "1", features = ["macros"] }
23+
24+
#shared lib
25+
pizza_lib = { path = "../pizza_lib" }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use lambda_runtime::{service_fn, Error, LambdaEvent};
2+
use pizza_lib::Pizza;
3+
use serde_json::{json, Value};
4+
5+
#[tokio::main]
6+
async fn main() -> Result<(), Error> {
7+
tracing_subscriber::fmt()
8+
.with_max_level(tracing::Level::INFO)
9+
.with_target(false)
10+
.with_ansi(false)
11+
.without_time()
12+
.init();
13+
let func = service_fn(func);
14+
lambda_runtime::run(func).await?;
15+
Ok(())
16+
}
17+
18+
async fn func(_: LambdaEvent<Value>) -> Result<(), Error> {
19+
// read the queue url from the environment
20+
let queue_url = std::env::var("QUEUE_URL").expect("could not read QUEUE_URL");
21+
22+
// let's create our pizza
23+
let message = Pizza {
24+
name: "margherita".to_string(),
25+
toppings: vec![
26+
"San Marzano Tomatoes".to_string(),
27+
"Fresh Mozzarella".to_string(),
28+
"Basil".to_string(),
29+
],
30+
};
31+
32+
// create our SQS client
33+
let config = aws_config::from_env().load().await;
34+
35+
// send our message to SQS
36+
let client = aws_sdk_sqs::Client::new(&config);
37+
client
38+
.send_message()
39+
.queue_url(queue_url)
40+
.message_body(json!(message).to_string())
41+
.send()
42+
.await?;
43+
44+
Ok(())
45+
}

0 commit comments

Comments
 (0)