• This repository has been archived on 14/Jun/2019
  • Stars
    star
    197
  • Rank 197,722 (Top 4 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Wrapper to simplify writing AWS Lambda functions in Rust (using the Python execution environment)

rust-crowbar

Build Status crates.io docs.rs

logo

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.3"
cpython = "0.2"

Use macros from both crates:

#[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.

Build Environment

It is notoriously difficult to build a properly-linked shared library against the Lambda execution environment. Using either an Amazon Linux AMI or the Amazon Linux Docker image, even if using the exact version as Lambda, any package installations will likely upgrade OpenSSL from 1.0.1k to 1.0.2k, causing a linker exception at runtime. @naftulikay lost a ridiculous amount of time trying to statically compile, pin package versions, and try linker hacks.

The best solution available is to use the lambci/lambda:build-python3.6 Docker image which is built from an exact filesystem replica via tarballing the filesystem at runtime from a Python 3.6 runtime Lambda function. The authors went to extensive lengths to pin packages and replicate the environment as accurately as possible, and experience has shown this is the best way to build Python 3.6 shared libraries.

@naftulikay created a sample Rust build environment based on the upstream lambci/lambda:build-python3.6 image at naftulikay/crowbar. Previously, naftulikay/circleci-amazonlinux-rust was used and the aforementioned issues were encountered. Despite CircleCI being used in the name, the image is a fairly generic Rust build environment and should be fairly portable and resuable. For Travis CI and CircleCI examples, please look in the examples/ci directory.

Because you're building a dynamic library, other libraries that you're dynamically linking against need to also be in the Lambda execution environment. By using the lambci/lambda:build-python3.6 image, the build environment will be consistent with the runtime environment.

As described here, the Lambda execution environment uses a runtime library path equivalent to:

LD_LIBRARY_PATH=/lib64:/usr/lib64:$LAMBDA_TASK_ROOT:$LAMBDA_TASK_ROOT/lib

@naftulikay wrote a fairly naïve Python script which will recursively copy linked libraries into the deployment package under lib/. This ensures that any non-standard libraries will be available on the library path at runtime. See the examples/ci/{travis,circle} directories for examples on how to use this, and see naftulikay/docker-crowbar for more information.

Deployment

Serverless framework

Serverless framework is an extemely popular workflow tool for developing and deploying serverless applications and comes with in depth guides for AWS lambda. Serverless framework is surrounded by a strong ecosystem of plugins including a plugin for deploying Rust applications. A serverless rust template for quickly bootstraping, building, and deploying crowbar applications can be found here.

Contributing

crowbar welcomes your contributions:

  • Let us know if you use crowbar in production
  • If you have a bug report or an idea, submit an issue
  • If you want something to work on, check the issues list
  • Please submit non-trivial changes as an issue first; send a pull request when the implementation is agreed on

crowbar follows a code of conduct; please read it.

Alternatives

As AWS Lambda has added more runtimes, more ways to run Rust on Lambda have emerged.

The Rust on AWS Lambda project is kind enough to offer an alternative, and a comparison of itself to crowbar.

More Repositories

1

emojos.in

Shows custom emoji for Mastodon/Pleroma instances
Rust
136
star
2

bandcamp-dl

Download your Bandcamp collection
Python
61
star
3

pentacle

Executes programs as sealed anonymous files on Linux
Rust
41
star
4

eggbug-rs

Bot library for cohost.org
Rust
38
star
5

python-simplemediawiki

Extremely low-level wrapper to the MediaWiki API
Python
27
star
6

dotfiles

novel and fascinating ways to make computers not fucking work
Shell
20
star
7

esbat

Rust library for lunar phase calculations 🌒🌓🌔
Rust
19
star
8

mw

VCS-like nonsense for MediaWiki websites
Python
15
star
9

paws

a simple pause container 🐾
Dockerfile
13
star
10

html5nyancat

Looped nyan cat animation and audio built on <audio> and SVG
13
star
11

blaseball-scoreboard

Minimalist scoreboard for Blaseball
JavaScript
11
star
12

sshca

A bad SSH certificate authority
Rust
9
star
13

safeword

Gracefully ends a Tokio-based application upon receiving a signal
Rust
8
star
14

rust-cpython-json

Converts native Python objects into serde_json Values and back again
Rust
7
star
15

ipv6-is-okay

Slides for an introductory talk I gave about IPv6 to my team in Jan 2017
6
star
16

blaseball-archive-scripts

JavaScript
6
star
17

python-thefuckingweather

Python API for The Fucking Weather (unofficial)
Python
6
star
18

ublock-rules

Moved to https://buttslol.net/ublock-filters.txt
5
star
19

vitellary

[WIP] VVVVVV autosplitter for macOS and Linux
Rust
5
star
20

minlambda

A minimalist AWS Lambda runtime for Rust
Rust
5
star
21

mutual-gf

Fediverse display name moon phase updater
Rust
5
star
22

fedora-elections-flask

Python
5
star
23

before

🩸 Do you remember before? 🩸
Rust
5
star
24

20020-map

Mapping project for the Bowl Game, as seen in 20020 (Jon Bois/Secret Base)
Rust
4
star
25

tokio-jsoncodec

Integration between tokio-codec and serde_json
Rust
3
star
26

iliana.fyi

helscome my wedsite. its not done.
JavaScript
3
star
27

dockerfiles

[WHALE EMOJI] [PEACH EMOJI]
Shell
3
star
28

io-window

Seekable Rust I/O adapter that limits operations to a byte range
Rust
2
star
29

staticstat

Rust
2
star
30

router-setup

whole-house wireguard!!
2
star
31

bricks

Rust
2
star
32

aoc2021

#![no_std] rust solutions to advent of code 2021 until i get distracted by something else
Rust
2
star
33

breadboard

A JSON API router in Rust
Rust
2
star
34

virt-builder-scripts

Shell
2
star
35

aoc2022

how many days will xie actually finish before getting distracted this time???
Rust
2
star
36

aoc2019

Rust
2
star
37

mdrecord

💽 netmd dingus
TypeScript
2
star
38

iliana

2
star
39

newbot

a silly fediverse emoji bot
Rust
2
star
40

form-data-builder

Simple multipart/form-data (RFC 7578) document builder
Rust
2
star
41

striped

y'know, like SOCKS? get it?
Go
1
star
42

lambda-http-local

A small wrapper for locally developing HTTP handlers for AWS Lambda
Rust
1
star
43

lego-httpreq-server

Rust
1
star
44

bracketeering

Score and display NCAA basketball bracket pools
Python
1
star
45

nixos-configs

🚚 MOVED 🚚 https://git.iliana.fyi/nixos-configs
Nix
1
star
46

aoc2018

Rust
1
star
47

promotional-teflon-kittenbasket

A clean and minimal website base to showcase ideas
1
star
48

cloud-init-lite

‮the cloud
Shell
1
star
49

there-are-too-many-things-in-the-computer-box

a tool to tell me how big some things are on the github actions runner instances
1
star
50

yankisrc

Grab ISRC data from Spotify and throw it into MusicBrainz
Python
1
star
51

flacshim

Python
1
star
52

gnupg-win32-proxy

If you find a reason to use this, I'm deeply sorry
Python
1
star
53

simx

Local Programmer Rolls Boulder Up Hill Again
Rust
1
star
54

fedora-business-cards

Business card generator
Python
1
star
55

sans-anaconda

A manual for installing Red Hat-like distributions without Anaconda
Python
1
star