-
Notifications
You must be signed in to change notification settings - Fork 348
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
Allow running handler with a callback #694
Closed
Closed
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
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,19 @@ | ||
[package] | ||
name = "basic-lambda-with-callback" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
||
# Use cargo-edit(https://github.com/killercup/cargo-edit#installation) | ||
# to manage dependencies. | ||
# 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] | ||
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"] } | ||
tokio-test = "0.4.2" |
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,11 @@ | ||
# 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` | ||
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,61 @@ | ||
// This example requires the following input to succeed: | ||
// { "command": "do something" } | ||
use std::sync::Arc; | ||
|
||
use lambda_runtime::{service_fn, Error, LambdaEvent}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// This is also a made-up example. Requests come into the runtime as unicode | ||
/// strings in json format, which can map to any structure that implements `serde::Deserialize` | ||
/// The runtime pays no attention to the contents of the request payload. | ||
#[derive(Deserialize)] | ||
struct Request { | ||
command: String, | ||
} | ||
|
||
/// This is a made-up example of what a response structure may look like. | ||
/// There is no restriction on what it can be. The runtime requires responses | ||
/// to be serialized into json. The runtime pays no attention | ||
/// to the contents of the response payload. | ||
#[derive(Serialize)] | ||
struct Response { | ||
req_id: String, | ||
msg: String, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
// required to enable CloudWatch error logging by the runtime | ||
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(); | ||
|
||
// Since the Lambda is fozen in between invocations you can use this callback | ||
// to e.g. flush traces/logs/metrics | ||
let cleanup = Arc::new(|| { | ||
println!("Callback called!"); | ||
}); | ||
|
||
|
||
let func = service_fn(my_handler); | ||
lambda_runtime::run_with_callback(func, cleanup).await?; | ||
Ok(()) | ||
} | ||
|
||
pub(crate) async fn my_handler(event: LambdaEvent<Request>) -> Result<Response, Error> { | ||
// extract some useful info from the request | ||
let command = event.payload.command; | ||
|
||
// prepare the response | ||
let resp = Response { | ||
req_id: event.context.request_id, | ||
msg: format!("Command {} executed.", command), | ||
}; | ||
|
||
// return `Response` (it will be serialized to JSON automatically by the runtime) | ||
Ok(resp) | ||
} |
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
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.
can you do the following to avoid the clone?