lambda-web

Run Actix web, Rocket, Warp on AWS Lambda

crates.io API docs

Features

Web frameworks

Supported

AWS infrastructure

Supported

Not supported

Example

Actix Web

Cargo.toml

```toml [[bin]] name = "bootstrap" path = "src/main.rs"

[dependencies] lambda-web = { version = "0.1.6", features=["actix4"] } ```

main.rs

```rust use lambdaweb::actixweb::{self, get, App, HttpServer, Responder}; use lambdaweb::{isrunningonlambda, runactixon_lambda, LambdaError};

[get("/")]

async fn hello() -> impl Responder { format!("Hello") }

[actix_web::main]

async fn main() -> Result<(),LambdaError> { let factory = move || { App::new().service(hello) };

if is_running_on_lambda() {
    // Run on AWS Lambda
    run_actix_on_lambda(factory).await?;
} else {
    // Local server
    HttpServer::new(factory)
        .bind("127.0.0.1:8080")?
        .run()
        .await?;
}
Ok(())

} ```

Rocket

Cargo.toml

```toml [[bin]] name = "bootstrap" path = "src/main.rs"

[dependencies] lambda-web = { version = "0.1.6", features=["rocket05"] } rocket = "0.5.0-rc.1" ```

main.rs

```rust use rocket::{self, get, routes}; use lambdaweb::{isrunningonlambda, launchrocketon_lambda, LambdaError};

[get("/hello//")]

fn hello(name: &str, age: u8) -> String { format!("Hello, {} year old named {}!", age, name) }

[rocket::main]

async fn main() -> Result<(), LambdaError> { let rocket = rocket::build().mount("/", routes![hello]); if isrunningonlambda() { // Launch on AWS Lambda launchrocketonlambda(rocket).await?; } else { // Launch local server rocket.launch().await?; } Ok(()) } ```

Warp

Cargo.toml

```toml [[bin]] name = "bootstrap" path = "src/main.rs"

[dependencies] lambda-web = { version = "0.1.6", features=["warp03"] } tokio = { version = "1" } ```

main.rs

```rust use lambdaweb::warp::{self, Filter}; use lambdaweb::{isrunningonlambda, runwarponlambda, LambdaError};

[tokio::main]

async fn main() -> Result<(), LambdaError> { // GET /hello/warp => 200 OK with body "Hello, warp!" let hello = warp::path!("hello" / String).map(|name| format!("Hello, {}", name));

if is_running_on_lambda() {
    // Run on AWS Lambda
    run_warp_on_lambda(warp::service(hello)).await?;
} else {
    // Run local server
    warp::serve(hello).run(([127, 0, 0, 1], 8080)).await;
}
Ok(())

} ```

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 .

```console $ 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.

```console $ cd yourappcratedir $ docker run -it --rm -v ~/.cargo/registry:/root/.cargo/registry:z -v .:/build:z lambdabuilder ...

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.

toml [[bin]] name = "bootstrap" path = "src/main.rs"

Setup AWS Lambda & API gateway

Lambda

API Gateway