Crate crowbar [] [src]

crowbar makes it easy to write AWS Lambda functions in Rust. It wraps native Rust functions into CPython modules that handle converting Python objects into Rust objects and back again.

Usage

Add both crowbar and cpython to your Cargo.toml:

[dependencies]
crowbar = "0.2"
cpython = "0.1"

Use macros from both crates:

Be careful when using this code, it's not being tested!
#[macro_use(lambda)]
extern crate crowbar;
#[macro_use]
extern crate cpython;

And write your function using the lambda! macro:

lambda!(|event, context| {
    println!("hi cloudwatch logs, this is {}", context.function_name());
    // return the event without doing anything with it
    Ok(event)
});

Building Lambda functions

For your code to be usable in AWS Lambda's Python execution environment, you need to compile to a dynamic library with the necessary functions for CPython to run. The lambda! 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 lambda! macro as above, you need to use lambda for the library name (see the documentation for lambda! if you want to use something else).

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

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.

The builder directory of the crowbar git repo contains a Dockerfile with Rust set up and a build script to dump a zip file containing a stripped shared library to stdout. Documentation for using that is available at ilianaw/crowbar-builder on Docker Hub.

Building for Python 2.7

Python 2.7 support for crowbar is deprecated and will be removed in a future release, and at least by crowbar 1.0.

There are a multitude of reasons to not use Python 2; even more so for projects written in Rust that happen to use Python as a shim. When crowbar was first released, AWS Lambda did not yet provide a Python 3 execution environment; it would not have Python 2.7 support today if timelines had crossed better.

Nonetheless, here's the incantation you need in your Cargo.toml to build for the Python 2.7 execution environment:

[dependencies]
crowbar = { version = "0.2", default-features = false }
cpython = { version = "0.1", default-features = false, features = ["python27-sys"] }

Macros

lambda

Macro to wrap a Lambda function handler.

Structs

LambdaContext

Provides a view into the context object available to Lambda functions.

Enums

ContextError

Error enum for things that can go wrong while processing the context object.

Value

Represents any valid JSON value.

Type Definitions

LambdaResult

Result object that accepts Ok(T) or any Err(Error).