You will need the Netlify CLI for this lesson as well as a Netlify account.
On Mac you can get the CLI via homebrew or if you have npm
installed you can use npm i -g netlify-cli
.
brew install netlify-cli
Netlify has a CI system that will build Rust for us, but we're going to build and deploy our functions manually.
Create a new Netlify site that isn't tied to any GitHub repo using netlify sites:create
.
netlify sites:create
You will have to answer a couple questions. None of the questions matter for this lesson, so choose whatever you want.
This process will create a .netlify/state.json
file with a siteId
in it. This is what links your local development to the Netlify site.
{
"siteId": "75e8be9f-7935-4013-91ba-0c57017696a6"
}
To deploy to Netlify Functions we'll need a netlify.toml
.
We'll set our functions directory to functions/
. This is where we'll put our binaries after we build them to be deployed as serverless functions.
[functions]
directory = "functions/"
[build]
publish = "crates"
make sure you create the functions directory
mkdir functions
We replace cargo build
with cargo lambda build
which is a drop-in replacement and should behave largely the same. This handles cross-compilation for us, since Netlify/AWS' lambda environment is Amazon Linux 2, not MacOS or Windows.
cargo lambda build --bin pokemon-api
When we run the build, cargo lambda
places the built binaries in target/lambda/<binary-name>/bootstrap
. This is because this is the directory layout needed to deploy directly to AWS Lambda. We can use that knowledge to copy the resulting binary into the functions directory.
cp target/lambda/pokemon-api/bootstrap functions/pokemon-api
Finally I've chosen to use the crates
directory, which is all of the source code, as the publish
directory. This will serve up all of our code publicly, so you might want to choose a different directory.
Once built, we can deploy to a preview using netlify deploy
or to production using --prod
.
netlify deploy --prod
❯ netlify deploy --prod
Deploy path: /rust-adventure/pokemon-api-netlify/crates
Functions path: /rust-adventure/pokemon-api-netlify/functions
Configuration path: /rust-adventure/pokemon-api-netlify/netlify.toml
Deploying to main site URL...
✔ No cached functions were found
✔ Finished hashing 9 files and 1 functions
✔ CDN requesting 0 files and 0 functions
✔ Finished uploading 0 assets
✔ Deploy is live!
Build logs: https://app.netlify.com/sites/site-name-84c264/deploys/650bd98b7b76cc1b8474ec69
Function logs: https://app.netlify.com/sites/site-name-84c264/functions
Unique deploy URL: https://650bd98b7b76cc1b8474ec69--site-name-84c264.netlify.app
Website URL: https://site-name-84c264.netlify.app
and your new serverless Rust function is deployed!
You can use the website URL plus the url path .netlify/functions/pokemon-api
to curl the function, or view it in a browser.
❯ curl https://site-name-84c264.netlify.app/.netlify/functions/pokemon-api
<html><body><h1>hello!</h1></body></html>
After hitting the function, you can also use the function logs url to view the logs from the production function.