aws lambda api gateway interfaces for Rustlang applications
#[macro_use] extern crate cpython;
#[macro_use] extern crate lando;
gateway!(|_, _| {
Ok(lando::Response::new("Hello, what have we here?"))
});
๐ง ๐ท๐ฟโโ๏ธ ๐ท๐ฝ ๐ทโโ๏ธ ๐ท ๐ง this project is currently active under construction. expect changes.
Lando is a crate for serverless rustlang HTTP applications.
The rustlang ecosystem has a number of really great HTTP server crates. If you're interested in writing HTTP applications, you may want to evaluate them as well. A common theme they share is in providing interfaces for authoring applications, in addition to interfaces for configuring servers that listen on ports that expose your application over network connections. A server which is then your reponsiblity to figure out how to host, scale, monitor and manage operations and uptime for.
Lando is different. Lando's focus is solely on writing applications. AWS will manage servers for you, freeing you from the business and toil of the undifferentiated heavy lifting that comes along with managing servers yourself.
Lando is designed to work with the interfaces of strong existing ecosystems, both within Rust as well as the strong serverless ecosystems that extend beyond Rust ( make some friends! ).
Lando's embraces the Rust community standard http crate as it's interface for api gateway. The http crate was extracted from the work of a number of successful projects and was designed as a framework-agnostistic and extensible http library. Lando extends the existing work of the crowbar crate which provides needed lower level machinery for easily embeding a Rust application with one of lamdba's lowest overhead runtimes, python 3.6. Lando specifically targets API Gateway triggered lambdas. Checkout crowbar for other types of lambda triggers.
A large and mature ecosystem of tooling for AWS lambda already exists and works well, including workflow tools like the serverless toolkit. Because these tools are likely to already exist within organizations, the barrier of introducing Rustlang into their arsenel will be much lower. Lando does not intend to replace these tools but instead to work well with them ๐ซ๐พ.
๐ก You may be asking yourself, what makes Rust a good choice for Lambda? The AWS cost model for lambda is largely based on two factors: memory size and speed. The CPU provided to applications is proportional to memory size requested. Lambda has a pay per usage cost model billing favoring applications that are both fast and have low memory overheads. As a systems language, Rust is designed specifically for these kinds of needs. Rust has a very tiny runtime, manages memory very effciently, and is extremely fast. . As a highly embeddable language, its interop story for runtimes like python's is ๐. Be mindful that lando assumes you're exposing these applications through AWS API gateway which has its own generous pricing model.
Add the following to your cargo project's Cargo.toml
file.
[lib]
name = "lambda"
crate-type = ["cdylib"]
[dependencies]
lando = "0.1"
cpython = "0.1"
๐ก You may be new to the
cdylib
andcrate-type
lib attributes. This informs rustc to link and produce a shared object (*.so
) file allowing your rustlang application to be embedded within the AWS python 3.6 lambda runtime
Lando exports a macro named gateway!
which in turn, injects a Rust function or
closure to a cpython initializer making it ready for use within an aws lambda.
#[macro_use] extern crate cpython;
#[macro_use] extern crate lando;
gateway!(|request, _context| {
println!("{:?}", request);
Ok(lando::Response::new(()))
});
This closure accepts an http::Request
with a lando::Body. This body can be dereferenced as
a slice of bytes.
For more more in-depth details see this project's crate documentation.
In order to deploy your app you will need to build it within a runtime compatible with the lambda python 3.6 env.
A docker image is provided for convenience
It's focus is on applications targetting stable versions of Rust.
$ docker run --rm \
-v ${PWD}:/code \
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
-v ${HOME}/.cargo/git:/root/.cargo/git \
-e CARGO_FLAGS="--features lando/python3-sys" \
softprops/lambda-rust
This will result in a deployable .so build artifact under a target/lambda
directory
This file can then be zipped up for AWS lambda deployment.
A serverless framework plugin exists to facilitate rapid development/deployment cycles.
The fastest way to get started with lando is by using this serverless project template
$ serverless install \
--url https://github.com/softprops/serverless-lando \
--name my-new-service
Doug Tangren (softprops) 2018