1 unstable release

Uses old Rust 2015

0.0.0 Jan 22, 2018

#91 in #serverless

MIT license

16KB
226 lines

gateway Build Status Coverage Status crates.io docs.rs

aws lambda gateway api trigger interfaces for Rustlang applications

Documentation

Roadmap

[x] expose typesafe interface for API gateway handlers
[ ] expose API gateway interface adapting to Rust's http crate

Doug Tangren (softprops) 2018


lib.rs:

Gateway extends the crowbar crate making it possible to write type safe AWS Lambda functions in Rust that are invoked by API gateway events.

It exports native Rust functions as CPython modules making it possible to embed handlers within aws's python3.6 runtime.

Usage

Add both gateway and cpython to your Cargo.toml:

[dependencies]
gateway = "0.1"
cpython = "0.1"

Use macros from both crates:

#[macro_use(gateway)]
extern crate gateway;
// the following imports macros needed by the gateway macro
#[macro_use]
extern crate cpython;

And write your function using the gateway! macro:

gateway!(|_request, context| {
    println!("hi cloudwatch logs, this is {}", context.function_name());
    // return a basic 200 response
    Ok(gateway::Response::default())
});

Packaging functions

For your code to be usable in AWS Lambda's Python3.6 execution environment, you need to compile to a dynamic library with the necessary functions for CPython to run. The gateway! macro does most of this for you, but cargo still needs to know what to do.

You can configure cargo to build a dynamic library with the following. If you're using the gateway! macro as above, you need to use lambda for the library name (see the documentation for gateway! if you want to use something else).

[lib]
name = "lambda"
crate-type = ["cdylib"]

Note: cdylib exports C interface from a Rust dynamic library.

Link formats are described here

cargo build will now build a liblambda.so. Put this in a zip file and upload it to an AWS Lambda function. Use the Python 3.6 execution environment with the handler configured as liblambda.handler.

Because you're building a dynamic library, other libraries that you're dynamically linking against need to also be in the Lambda execution environment. The easiest way to do this is building in an environment similar to Lambda's, such as Amazon Linux. You can use an EC2 instance or a Docker container.

Dependencies

~2–3MB
~68K SLoC