70 %
Chris Biscardi

Deploy your first Netlify Serverless function powered by Rust

Inititalize a new package with a binary crate and add lamedh_http.

lamedh is netlify's fork of the official AWS Lambda runtime for Rust

cargo init
cargo add lamedh_http

cargo add is provided by cargo-edit

The code to run a hello-world lambda uses a couple macros to make things easier:

  1. tokio::main for an async runtime
  2. lambda(http) to bootstrap the lambda function execution context
use lamedh_http::{
lambda::{lambda, Context},
IntoResponse, Request,
type Error =
Box<dyn std::error::Error + Send + Sync + 'static>;
async fn main(
_: Request,
_: Context,
) -> Result<impl IntoResponse, Error> {
dbg!("in main");

To build for the runtime Netlify expects, we can use x86_64-unknown-linux-musl installed via rustup. When we cargo build we'll specify the target as well as release. We specify release here because we want a smaller end artifact to upload and debug releases can be quite large.

rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl --release

Troubleshooting MacOS Problems

If you're on macos and you get this linker error message:

error: linking with `cc` failed: exit code: 1

then you need to install some cross-compilation tooling.

brew install FiloSottile/musl-cross/musl-cross

and then tell cargo to use the linker by specifying the following in ./.cargo/config.

linker = "x86_64-linux-musl-gcc"

Deploying to Netlify

Now after building, we can find the binary at


We'll deal with renaming the binary in a second, but first we can test the output on netlify by copying the binary into the ./functions folder and renaming it to something like hello-world

cp ./target/x86_64-unknown-linux-musl/release/<name-of-project> ./functions/hello-world

Deploying to netlify will send our function up to a deploy preview, which lets us test our function without deploying to our primary live site.

netlify deploy

You should see something like this, which indicates one new function has been deployed.

Deploying to draft URL...
✔ Finished hashing 2 files and 1 functions
✔ CDN requesting 0 files and 1 functions
✔ Finished uploading 1 assets
✔ Deploy is live!

Netlify doesn't hand us the location of our function URLs, so we'll have to construct them ourselves or view the function in the netlify dashboard. Using curl or your favorite http request tool (like postman). The url will be the Website Draft URL plus .netlify/functions and finally the name of our function. We should get a response "boop".

curl https://603d80e57ec4b20b2c63a3e1--discord-rust-slash-commands.netlify.app/.netlify/functions/hello-world

We can not currently view function logs for deploy previews (we'll cover implementing our over observability soon), so for now we need to deploy to production.

netlify deploy --prod

and now after curling the production URL we'll see the dbg! output.

4:52:51 PM: [src/main.rs:15] "in main" = "in main"
4:52:51 PM: Duration: 1.26 ms Memory Usage: 28 MB