lambda-web
Run Rust web server frameworks on AWS Lambda. Currently, it supports Actix web, axum, Rocket, warp.
Features
Supported web frameworks
Supported AWS infrastructure
Not supported
- API Gateway HTTP API with payload format version 1.0
- Application Load Balancer (ALB)
Example
Actix Web
Cargo.toml
[[]]
= "bootstrap"
= "src/main.rs"
[]
= { = "0.1.8", =["actix4"] }
main.rs
use ;
use ;
async
async
axum
Cargo.toml
[[]]
= "bootstrap"
= "src/main.rs"
[]
= { = "0.1.8", =["hyper"] }
= "0.3"
= { = "1" }
main.rs
use ;
use ;
use SocketAddr;
// basic handler that responds with a static string
async
async
Rocket
Cargo.toml
[[]]
= "bootstrap"
= "src/main.rs"
[]
= { = "0.1.8", =["rocket05"] }
= "0.5.0-rc.1"
main.rs
use ;
use ;
async
warp
Cargo.toml
[[]]
= "bootstrap"
= "src/main.rs"
[]
= { = "0.1.8", =["hyper"] }
= "0.3"
= { = "1" }
main.rs
use ;
use Filter;
async
Create deploy ZIP file
As of writing (Nov, 2021), we have two options to run Rust on AWS Lambda: Amazon Linux 2 custom runtime or Docker container image.
I recommend ZIP deploy to Amazon Linux 2 custom runtime (provided.al2
) because it's faster cold start time than container image.
To build Amazon Linux 2 compatible binary, see Deploy.md for more details.
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 (HTTP)
- Create HTTP API
- Create single route "$default" and attach Lambda integration. Make sure, payload format version is "2.0"
API Gateway (REST)
- Create REST API
- Create two resources:
- ANY method on route
/
and attach Lambda proxy integration. - ANY method on route
/{proxy+}
and attach Lambda proxy integration.
- ANY method on route
- In settings tab, add
*/*
binary media type. - Then, deploy API to stage.