Usage
First, you'll need a vercel.json
file in your project:
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@3.1.2"
}
}
}
A Vercel Function will be created for every file that matches api/**/*.rs
.
api/handler.rs
:
use serde_json::json;
use vercel_runtime::{
lambda_http::{http::StatusCode, Error as LambdaError, Response},
run, IntoResponse, ProxyError, ProxyRequest,
};
#[tokio::main]
async fn main() -> Result<(), LambdaError> {
run(handler).await?;
Ok(())
}
pub async fn handler(_req: ProxyRequest) -> Result<impl IntoResponse, ProxyError> {
let response = Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(
json!({
"message": "你好,世界"
})
.to_string(),
)?;
Ok(response)
}
Finally we need a Cargo.toml
file at the root of your repository.
[[bin]]
name = "handler"
path = "api/handler.rs"
Dependencies
This Builder supports installing dependencies defined in the Cargo.toml
file.
Furthermore, more system dependencies can be installed at build time with the presence of a shell build.sh
file in the same directory as the entrypoint file.
Local Development
With vercel dev
and vercel-rust
, you can develop your Rust-based lambdas on your own machine.
During local development with vercel dev
, ensure rust
and cargo
are already installed and available in your PATH
, since they will not be installed automatically. The recommended way to install is with rustup.
Contributing
Since this project contains both Rust and Node.js code, you need to install the relevant dependencies. If you're only working on the JavaScript side, you only need to install those dependencies (and vice-versa).
# install node dependencies
npm install
# install cargo dependencies
cargo fetch
Runtime Invocation Flowchart
graph TD
A["Lambda Invocation"] --> |"process_request(event: LambdaEvent<VercelEvent>) → ProxyRequest"| B[ProxyRequest]
B --> |"handler_fn(req: ProxyRequest) → Future<Output = Result<impl IntoResponse, ProxyError>>"| C["Runtime calls handler_fn"]
C --> |"Ok(r) => process_response(r)"| D["ProxyResponse"]
C --> |"Err(e) => process_error(e)"| E["ProxyError"]
Upon a request a LambdaEvent
containing a VercelEvent
is mapped to a ProxyRequest
which can be consumed in the handler function. The Result
of the handler function will be mapped into a ProxyResponse
or ProxyError
.
FAQ
- The Root
Cargo.toml
is setting up the workspaces for the endpoints. - The Endpoints (
api/**/handler.rs
) export a function with the signature: pub async fn handler(_req: ProxyRequest) -> Result<impl IntoResponse, ProxyError>
.
.
├── api
│ ├── endpoint_1
│ │ ├── Cargo.toml
│ │ └── src
│ │ └── main.rs
│ └── endpoint_2
│ ├── Cargo.toml
│ └── src
│ └── main.rs
├── Cargo.lock
└── Cargo.toml
What should be in the documentation
VERCEL_BUILDER_DEBUG=true
Merging Endpoints?
Builder
- Installs the Rust toolchain
- Injects a pre-defined
entry
package in the workspace that imports all endpoint handler
fn (proc macro) with runtime initialization and routing logic.
Event
[https://github.com/awslabs/aws-lambda-rust-runtime/releases/tag/v0.3.0](Base Lambda Runtime Release Notes)
files: {
"files": {
"api/Cargo.lock": {
"type": "FileFsRef",
"mode": 33188,
"fsPath": "/vercel/path0/api/Cargo.lock"
},
"api/Cargo.toml": {
"type": "FileFsRef",
"mode": 33188,
"fsPath": "/vercel/path0/api/Cargo.toml"
},
"api/user.rs": {
"type": "FileFsRef",
"mode": 33188,
"fsPath": "/vercel/path0/api/user.rs"
},
".vercelignore": {
"type": "FileFsRef",
"mode": 33188,
"fsPath": "/vercel/path0/.vercelignore"
},
"package.json": {
"type": "FileFsRef",
"mode": 33188,
"fsPath": "/vercel/path0/package.json"
},
"pnpm-lock.yaml": {
"type": "FileFsRef",
"mode": 33188,
"fsPath": "/vercel/path0/pnpm-lock.yaml"
},
"vercel.json": {
"type": "FileFsRef",
"mode": 33188,
"fsPath": "/vercel/path0/vercel.json"
}
},
"entrypoint": "api/user.rs",
"workPath": "/vercel/path0",
"repoRootPath": "/vercel/path0",
"config": {
"zeroConfig": true,
"functions": {
"api/**/*.rs": {
"runtime": "ecklf-tmp-runtime-test@1.0.4"
}
},
"projectSettings": {
"createdAt": 1674047862614,
"installCommand": null,
"buildCommand": null,
"devCommand": null,
"outputDirectory": null,
"rootDirectory": null,
"framework": null,
"nodeVersion": "18.x"
},
"framework": null,
"nodeVersion": "18.x"
},
"meta": {
"skipDownload": true,
"cliVersion": "28.12.7"
}
}