Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Calavera <david.calavera@gmail.com>
- Loading branch information
Showing
5 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,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"] } |
This file contains 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 @@ | ||
# 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` |
This file contains 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,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 | ||
} |
This file contains 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