-
Notifications
You must be signed in to change notification settings - Fork 361
Add an advanced SQS multiple functions with shared data example #720
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
Merged
calavera
merged 5 commits into
awslabs:main
from
maxday:maxday/advanced-sqs-multiple-functions-shared-data
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
examples/advanced-sqs-multiple-functions-shared-data/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[workspace] | ||
|
||
members = [ | ||
"producer", | ||
"consumer", | ||
"pizza_lib", | ||
] | ||
|
||
[profile.release] | ||
opt-level = 'z' | ||
lto = true | ||
codegen-units = 1 | ||
panic = 'abort' |
28 changes: 28 additions & 0 deletions
28
examples/advanced-sqs-multiple-functions-shared-data/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# AWS Lambda Function example | ||
|
||
## Build & Deploy | ||
|
||
1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda#installation) | ||
2. Build the function with `cargo lambda build --release` | ||
4. Make sure to edit the QUEUE_URL env variable in producer/Cargo.toml | ||
3. Deploy boths functions to AWS Lambda with | ||
|
||
`cargo lambda deploy consumer --iam-role YOUR_ROLE` | ||
|
||
`cargo lambda deploy producer --iam-role YOUR_ROLE` | ||
|
||
## Build for ARM 64 | ||
|
||
Build the function with `cargo lambda build --release --arm64` | ||
|
||
## Add the SQS trigger to the consumer function | ||
|
||
You can use aws-cli to create an event source mapping: | ||
|
||
```bash | ||
aws lambda create-event-source-mapping \ | ||
--function-name consumer \ | ||
--region <region> \ | ||
--event-source-arn <your-SQS-queue-ARN> \ | ||
--batch-size 1 | ||
``` |
22 changes: 22 additions & 0 deletions
22
examples/advanced-sqs-multiple-functions-shared-data/consumer/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "consumer" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
||
[dependencies] | ||
#tracing | ||
tracing = "0.1.40" | ||
tracing-subscriber = "0.3.17" | ||
|
||
#aws dependencies | ||
aws-sdk-config = "0.35.0" | ||
aws-sdk-sqs = "0.35.0" | ||
aws_lambda_events = { version = "0.11.1", features = ["sqs"], default-features = false } | ||
|
||
#lambda runtime | ||
lambda_runtime = "0.8.1" | ||
tokio = { version = "1", features = ["macros"] } | ||
|
||
#shared lib | ||
pizza_lib = { path = "../pizza_lib" } |
24 changes: 24 additions & 0 deletions
24
examples/advanced-sqs-multiple-functions-shared-data/consumer/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use aws_lambda_events::event::sqs::SqsEventObj; | ||
use lambda_runtime::{service_fn, Error, LambdaEvent}; | ||
use pizza_lib::Pizza; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
.with_target(false) | ||
.with_ansi(false) | ||
.without_time() | ||
.init(); | ||
let func = service_fn(func); | ||
lambda_runtime::run(func).await?; | ||
Ok(()) | ||
} | ||
|
||
async fn func(event: LambdaEvent<SqsEventObj<Pizza>>) -> Result<(), Error> { | ||
for record in event.payload.records.iter() { | ||
let pizza = &record.body; | ||
println!("Pizza name: {} with toppings: {:?}", pizza.name, pizza.toppings); | ||
} | ||
Ok(()) | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/advanced-sqs-multiple-functions-shared-data/pizza_lib/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "pizza_lib" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
serde = { version = "1.0.191", features = ["derive"] } |
7 changes: 7 additions & 0 deletions
7
examples/advanced-sqs-multiple-functions-shared-data/pizza_lib/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Pizza { | ||
pub name: String, | ||
pub toppings: Vec<String>, | ||
} |
25 changes: 25 additions & 0 deletions
25
examples/advanced-sqs-multiple-functions-shared-data/producer/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "producer" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[package.metadata.lambda.deploy] | ||
env = { "QUEUE_URL" = "https://changeMe" } | ||
|
||
[dependencies] | ||
#tracing | ||
tracing = "0.1.40" | ||
tracing-subscriber = "0.3.17" | ||
|
||
#aws dependencies | ||
aws-config = "0.57.1" | ||
aws-sdk-config = "0.35.0" | ||
aws-sdk-sqs = "0.35.0" | ||
|
||
#lambda runtime | ||
lambda_runtime = "0.8.1" | ||
serde_json = "1.0.108" | ||
tokio = { version = "1", features = ["macros"] } | ||
|
||
#shared lib | ||
pizza_lib = { path = "../pizza_lib" } |
61 changes: 61 additions & 0 deletions
61
examples/advanced-sqs-multiple-functions-shared-data/producer/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use lambda_runtime::{service_fn, Error, LambdaEvent}; | ||
use pizza_lib::Pizza; | ||
use serde_json::{json, Value}; | ||
|
||
struct SQSManager { | ||
client: aws_sdk_sqs::Client, | ||
queue_url: String, | ||
} | ||
|
||
impl SQSManager { | ||
fn new(client: aws_sdk_sqs::Client, queue_url: String) -> Self { | ||
Self { client, queue_url } | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
.with_target(false) | ||
.with_ansi(false) | ||
.without_time() | ||
.init(); | ||
|
||
// read the queue url from the environment | ||
let queue_url = std::env::var("QUEUE_URL").expect("could not read QUEUE_URL"); | ||
// build the config from environment variables (fed by AWS Lambda) | ||
let config = aws_config::from_env().load().await; | ||
// create our SQS Manager | ||
let sqs_manager = SQSManager::new(aws_sdk_sqs::Client::new(&config), queue_url); | ||
let sqs_manager_ref = &sqs_manager; | ||
|
||
// no need to create a SQS Client for each incoming request, let's use a shared state | ||
let handler_func_closure = |event: LambdaEvent<Value>| async move { | ||
process_event(event, sqs_manager_ref).await | ||
}; | ||
lambda_runtime::run(service_fn(handler_func_closure)).await?; | ||
Ok(()) | ||
} | ||
|
||
async fn process_event(_: LambdaEvent<Value>, sqs_manager: &SQSManager) -> Result<(), Error> { | ||
// let's create our pizza | ||
let message = Pizza { | ||
name: "margherita".to_string(), | ||
toppings: vec![ | ||
"San Marzano Tomatoes".to_string(), | ||
"Fresh Mozzarella".to_string(), | ||
"Basil".to_string(), | ||
], | ||
}; | ||
// send our message to SQS | ||
sqs_manager | ||
.client | ||
.send_message() | ||
.queue_url(&sqs_manager.queue_url) | ||
.message_body(json!(message).to_string()) | ||
.send() | ||
.await?; | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to fully show the possibilities of the workspace model, you might consider putting these dependencies that are shared between producer and consumer in workspace dependencies.
https://doc.rust-lang.org/nightly/cargo/reference/workspaces.html#the-dependencies-table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I just wanted to show how to share a struct, so I feel that this change might confuse the user? WDYT? I don't have a strong opinion here so let me know if you want me to make that change :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't feel strongly about it