lambda-web
Run Actix web, Rocket, Warp on AWS Lambda
Features
Web frameworks
Supported
AWS infrastructure
Supported
Not supported
- API Gateway HTTP API with payload format version 1.0
- API Gateway REST API
- Application Load Balancer (ALB)
Example
Actix Web
Cargo.toml
[[]]
= "bootstrap"
= "src/main.rs"
[]
= { = "0.1.0", =["actix4"] }
main.rs
use ;
use ;
async
async
Rocket
Cargo.toml
[[]]
= "bootstrap"
= "src/main.rs"
[]
= { = "0.1.2", =["rocket05"] }
= "0.5.0-rc.1"
main.rs
use ;
use ;
async
Warp
Cargo.toml
[[]]
= "bootstrap"
= "src/main.rs"
[]
= { = "0.1.1", =["warp03"] }
= { = "1" }
main.rs
use ;
use ;
async
Create deploy ZIP file
Currentry (Jun 2021), we have two options to run Rust on AWS Lambda: Amazon Linux 2 custom runtime or Docker container image.
I recommend Amazon Linux 2 custom runtime deploy because it's faster cold start time than container image.
To build Amazon Linux 2 compatible binary, it's better to build inside container. First, build Amazon Linux 2 container with Rust toolchain. This repository contains sample Dockerfile .
$ git clone https://github.com/hanabu/lambda-web
...
$ docker build -t lambda_builder lambda-web/docker
...
or
$ buildah bud -t lambda_builder lambda-web/docker
...
Once you get lambda_builder image, then build your code with Amazon Linux.
$ cd your_app_crate_dir
$ docker run -it --rm -v ~/.cargo/registry:/root/.cargo/registry:z -v .:/build:z lambda_builder
...
or
$ podman run -it --rm -v ~/.cargo/registry:/root/.cargo/registry:z -v .:/build:z lambda_builder
...
Then, you get deploy ZIP package in your_app_crate_dir/target_lambda/deploy.zip .
Make sure, your Cargo.toml has bootstrap
binary name.
[[]]
= "bootstrap"
= "src/main.rs"
Setup AWS Lambda & API gateway
Lambda
- Create lambda function with
provided.al2
custom runtime. Choose "Provide your own bootstrap on Amazon Linux 2" . - Upload ZIP file described above.
- IAM role, memory settings, etc. are as your demands.
As sample code above consumes only 30MB of memory, many simple Rust app can fit in 128MB setting.
API Gateway
- Create HTTP API
- Create single route "$default" and attach Lambda integration. Make sure, payload format version is "2.0"