├── .gitignore ├── Cargo.toml ├── README.md ├── package-lock.json ├── package.json ├── serverless.yml └── src ├── lib.rs └── utils.rs /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .serverless 3 | bin 4 | pkg 5 | target 6 | wasm-pack.log 7 | Cargo.lock -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-lambda" 3 | version = "0.1.0" 4 | authors = ["Colin Eberhardt "] 5 | 6 | [lib] 7 | crate-type = ["cdylib", "rlib"] 8 | 9 | [features] 10 | default = ["console_error_panic_hook"] 11 | 12 | [dependencies] 13 | cfg-if = "1.0.0" 14 | wasm-bindgen = "0.2" 15 | js-sys = "0.3" 16 | 17 | # The `console_error_panic_hook` crate provides better debugging of panics by 18 | # logging them with `console.error`. This is great for development, but requires 19 | # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for 20 | # code size when deploying. 21 | console_error_panic_hook = { version = "0.1.1", optional = true } 22 | 23 | # `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size 24 | # compared to the default allocator's ~10K. It is slower than the default 25 | # allocator, however. 26 | # 27 | # Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. 28 | wee_alloc = { version = "0.4.2", optional = true } 29 | 30 | [dev-dependencies] 31 | wasm-bindgen-test = "0.2" 32 | 33 | [profile.release] 34 | # Tell `rustc` to optimize for small code size. 35 | opt-level = "s" 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rust-webassembly-serverless 2 | 3 | This is a simple example of a Lambda function written entirely in Rust - compiled to WebAssembly, as detailed in [this blog post](https://blog.scottlogic.com/2018/10/18/serverless-rust.html). 4 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rust-lambda-serverless", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "rust-lambda-serverless", 9 | "version": "1.0.0", 10 | "license": "MIT" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rust-lambda-serverless", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "scripts": { 7 | "build": "wasm-pack build --target nodejs", 8 | "run:local": "serverless invoke local -f hello", 9 | "run": "serverless invoke -f hello", 10 | "deploy": "serverless deploy" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: rust-lambda-serverless 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs12.x 6 | region: us-east-2 7 | lambdaHashingVersion: [20201221] 8 | 9 | 10 | functions: 11 | hello: 12 | handler: pkg/rust_lambda.handler 13 | events: 14 | - http: 15 | path: greet 16 | method: get -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate cfg_if; 2 | extern crate wasm_bindgen; 3 | extern crate js_sys; 4 | 5 | mod utils; 6 | 7 | use js_sys::{Object, Reflect, Promise}; 8 | use wasm_bindgen::prelude::*; 9 | 10 | pub fn response(status: u8, body: String) -> Object { 11 | let object = Object::new(); 12 | Reflect::set(&object.as_ref(), &"statusCode".into(), &status.into()).unwrap(); 13 | Reflect::set(&object.as_ref(), &"body".into(), &body.into()).unwrap(); 14 | return object; 15 | } 16 | 17 | #[wasm_bindgen] 18 | pub fn handler() -> Promise { 19 | let mut string = String::new(); 20 | string.push_str("Hello, rust-lambda!"); 21 | let res = response(200, string); 22 | return Promise::resolve(res.as_ref()); 23 | } 24 | -------------------------------------------------------------------------------- /src/utils.rs: -------------------------------------------------------------------------------- 1 | use cfg_if::cfg_if; 2 | 3 | cfg_if! { 4 | // When the `console_error_panic_hook` feature is enabled, we can call the 5 | // `set_panic_hook` function at least once during initialization, and then 6 | // we will get better error messages if our code ever panics. 7 | // 8 | // For more details see 9 | // https://github.com/rustwasm/console_error_panic_hook#readme 10 | if #[cfg(feature = "console_error_panic_hook")] { 11 | extern crate console_error_panic_hook; 12 | pub use self::console_error_panic_hook::set_once as set_panic_hook; 13 | } else { 14 | #[inline] 15 | pub fn set_panic_hook() {} 16 | } 17 | } 18 | 19 | cfg_if! { 20 | // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global 21 | // allocator. 22 | if #[cfg(feature = "wee_alloc")] { 23 | extern crate wee_alloc; 24 | #[global_allocator] 25 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 26 | } 27 | } 28 | --------------------------------------------------------------------------------