AWS Lambdas in Rust made simple.
serverless-rust
Add the following to your Cargo.toml
file.
toml
[dependencies]
vicuna = "0.1.0"
💡 This crate is intended to be paired with the
serverless-rust
plugin.
Vicuna produces handlers which take in a Lambda request and produce an
appropriate response. The simplest handler is the default_handler
provided by
the crate:
```rust use lambdahttp::lambda; use vicuna::defaulthandler;
fn main() { lambda!(default_handler()) } ```
Handlers can be composed from middleware which can handle the request-response lifecycle in an arbitrary fashion. For example, a middleware that adds a header to our response could look like this:
```rust use lambda_http::http::header::{HeaderName, HeaderValue}; use vicuna::Handler;
fn addheader(handler: Handler) -> Handler { Box::new(move |req| { // Resolve any upstream middleware into a response. let mut resp = handler(req)?; // Add our custom header to the response. resp.headersmut().insert( HeaderName::fromstatic("x-hello"), HeaderValue::fromstatic("world"), ); Ok(resp) }) } ```
Middleware are wrapped around handlers, which themselves produce a handler for chainable invocation:
```rust use lambdahttp::{lambda, IntoResponse, Request, Response}; use lambdaruntime::{error::HandlerError, Context}; use vicuna::{Handle, WrapWith};
fn hellolambda(req: Request, _: Context) -> Result
fn main() { lambda!(hello_lambda) } ```