-
Notifications
You must be signed in to change notification settings - Fork 361
Lambda Extensions #376
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
Merged
Lambda Extensions #376
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
046d380
Create new Runtime Client crate
calavera 19e1ceb
Add Lambda Extension crate
calavera 9d46537
Rename client crate to avoid confusion.
calavera a7161be
Modify user API.
calavera af95f36
Cleanup user API.
calavera c5b423a
Add documentation and cleanup code.
calavera 23b3631
Make custom trait example more useful.
calavera 36cdbd8
Fix formatting.
calavera 6110926
Remove unused dependencies.
calavera f607efa
Add README files for the new crates.
calavera bbde541
Update readme files.
calavera 981b7ce
Fix extension name
calavera 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 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
[workspace] | ||
members = [ | ||
"lambda-http", | ||
"lambda-runtime" | ||
"lambda-runtime-api-client", | ||
"lambda-runtime", | ||
"lambda-extension" | ||
] |
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
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,31 @@ | ||
[package] | ||
name = "lambda_extension" | ||
version = "0.1.0" | ||
calavera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
edition = "2021" | ||
authors = ["David Calavera <[email protected]>"] | ||
description = "AWS Lambda Extension API" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/awslabs/aws-lambda-rust-runtime" | ||
categories = ["web-programming::http-server"] | ||
keywords = ["AWS", "Lambda", "API"] | ||
readme = "README.md" | ||
|
||
[dependencies] | ||
tokio = { version = "1.0", features = ["macros", "io-util", "sync", "rt-multi-thread"] } | ||
hyper = { version = "0.14", features = ["http1", "client", "server", "stream", "runtime"] } | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = "^1" | ||
bytes = "1.0" | ||
http = "0.2" | ||
async-stream = "0.3" | ||
tracing = { version = "0.1", features = ["log"] } | ||
tower-service = "0.3" | ||
tokio-stream = "0.1.2" | ||
lambda_runtime_api_client = { version = "0.4", path = "../lambda-runtime-api-client" } | ||
|
||
[dev-dependencies] | ||
tracing-subscriber = "0.3" | ||
once_cell = "1.4.0" | ||
simple_logger = "1.6.0" | ||
log = "^0.4" | ||
simple-error = "0.2" |
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,58 @@ | ||
# Runtime Extensions for AWS Lambda in Rust | ||
|
||
[](https://docs.rs/lambda_extension) | ||
|
||
**`lambda-extension`** is a library that makes it easy to write [AWS Lambda Runtime Extensions](https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html) in Rust. | ||
|
||
## Example extension | ||
|
||
The code below creates a simple extension that's registered to every `INVOKE` and `SHUTDOWN` events, and logs them in CloudWatch. | ||
|
||
```rust,no_run | ||
use lambda_extension::{extension_fn, Error, NextEvent}; | ||
use log::LevelFilter; | ||
use simple_logger::SimpleLogger; | ||
use tracing::info; | ||
|
||
async fn log_extension(event: NextEvent) -> Result<(), Error> { | ||
match event { | ||
NextEvent::Shutdown(event) => { | ||
info!("{}", event); | ||
} | ||
NextEvent::Invoke(event) => { | ||
info!("{}", event); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); | ||
|
||
let func = extension_fn(log_extension); | ||
lambda_extension::run(func).await | ||
} | ||
``` | ||
|
||
## Deployment | ||
|
||
Lambda extensions can be added to your functions either using [Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html#using-extensions-config), or adding them to [containers images](https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html#invocation-extensions-images). | ||
|
||
Regardless of how you deploy them, the extensions MUST be compiled against the same architecture that your lambda functions runs on. | ||
|
||
### Building extensions | ||
|
||
Once you've decided which target you'll use, you can install it by running the next `rustup` command: | ||
|
||
```bash | ||
$ rustup target add x86_64-unknown-linux-musl | ||
``` | ||
|
||
Then, you can compile the extension against that target: | ||
|
||
```bash | ||
$ cargo build -p lambda_extension --example basic --release --target x86_64-unknown-linux-musl | ||
``` | ||
|
||
This previous command will generate a binary file in `target/x86_64-unknown-linux-musl/release/examples` called `basic`. When the extension is registered with the [Runtime Extensions API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html#runtimes-extensions-api-reg), that's the name that the extension will be registered with. If you want to register the extension with a different name, you only have to rename this binary file and deploy it with the new name. |
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 @@ | ||
use lambda_extension::{extension_fn, Error, NextEvent}; | ||
use log::LevelFilter; | ||
use simple_logger::SimpleLogger; | ||
|
||
async fn my_extension(event: NextEvent) -> Result<(), Error> { | ||
match event { | ||
NextEvent::Shutdown(_e) => { | ||
// do something with the shutdown event | ||
} | ||
NextEvent::Invoke(_e) => { | ||
// do something with the invoke event | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
// required to enable CloudWatch error logging by the runtime | ||
// can be replaced with any other method of initializing `log` | ||
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); | ||
|
||
let func = extension_fn(my_extension); | ||
nmoutschen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lambda_extension::run(func).await | ||
} |
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,30 @@ | ||
use lambda_extension::{extension_fn, Error, NextEvent, Runtime}; | ||
use log::LevelFilter; | ||
use simple_logger::SimpleLogger; | ||
|
||
async fn my_extension(event: NextEvent) -> Result<(), Error> { | ||
match event { | ||
NextEvent::Shutdown(_e) => { | ||
// do something with the shutdown event | ||
} | ||
_ => { | ||
// ignore any other event | ||
// because we've registered the extension | ||
// only to receive SHUTDOWN events | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
nmoutschen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// required to enable CloudWatch error logging by the runtime | ||
// can be replaced with any other method of initializing `log` | ||
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); | ||
|
||
let func = extension_fn(my_extension); | ||
|
||
let runtime = Runtime::builder().with_events(&["SHUTDOWN"]).register().await?; | ||
|
||
runtime.run(func).await | ||
} |
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,36 @@ | ||
use lambda_extension::{run, Error, Extension, InvokeEvent, NextEvent}; | ||
use log::LevelFilter; | ||
use simple_logger::SimpleLogger; | ||
use std::{ | ||
future::{ready, Future}, | ||
pin::Pin, | ||
}; | ||
|
||
#[derive(Default)] | ||
struct MyExtension { | ||
data: Vec<InvokeEvent>, | ||
} | ||
|
||
impl Extension for MyExtension { | ||
type Fut = Pin<Box<dyn Future<Output = Result<(), Error>>>>; | ||
fn call(&mut self, event: NextEvent) -> Self::Fut { | ||
match event { | ||
NextEvent::Shutdown(_e) => { | ||
self.data.clear(); | ||
} | ||
NextEvent::Invoke(e) => { | ||
self.data.push(e); | ||
} | ||
} | ||
Box::pin(ready(Ok(()))) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
// required to enable CloudWatch error logging by the runtime | ||
// can be replaced with any other method of initializing `log` | ||
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); | ||
|
||
run(MyExtension::default()).await | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.