lambda-extension is a library that makes it easy to write AWS Lambda Runtime Extensions in Rust.
The code below creates a simple extension that's registered to every INVOKE and SHUTDOWN events, and logs them in CloudWatch.
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
}Lambda extensions can be added to your functions either using Lambda layers, or adding them to containers images.
Regardless of how you deploy them, the extensions MUST be compiled against the same architecture that your lambda functions runs on.
Once you've decided which target you'll use, you can install it by running the next rustup command:
$ rustup target add x86_64-unknown-linux-muslThen, you can compile the extension against that target:
$ cargo build -p lambda_extension --example basic --release --target x86_64-unknown-linux-muslThis 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, 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.