You will need the Netlify CLI for this lesson. 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
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]
command = """
mkdir -p functions &&
cargo zigbuild --release --bin pokemon-api --target x86_64-unknown-linux-gnu.2.26 &&
cp target/x86_64-unknown-linux-gnu/release/pokemon-api functions/
"""
publish = "crates"
The build command we'll set using a multiline toml string. We replace cargo build with cargo zigbuild which is a drop-in replacement and should behave largely the same. Cargo allows us to specify a --target, which we set to the target platform that Netlify Functions run on: x86_64-unknown-linux-gnu.
This means we're building on an m1 mac, a windows pc, or something else, but building in a way that lets our binary run on a different target platform.
cargo zigbuild enhances the --target flag with the ability to specify a glibc target as well. The glibc version we're targetting is 2.26, which is the version that the Netlify Functions operating system has.
Aside from cargo zigbuild the important pieces of the build command are that we're using the --release flag and that we're only building the pokemon-api binary.
When we run the build, cargo places the built binaries in target/x86_64-unknown-linux-gnu/release/, so we can use that knowledge to copy the resulting binary into the functions directory.
We also need to create the functions directory with mkdir so we can copy the binary into it. We use the -p flag to make sure that the mkdir still succeeds if the folder already exists.
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.
We'll also use a rust-toolchain.toml file in the root of our project to tell Cargo which version of Rust we want to use (and honestly, to remind ourselves). In this case we specify the current stable release of Rust, which can change over time and the x86_64-unknown-linux-gnu target.
[toolchain]
channel = "stable"
targets = ["x86_64-unknown-linux-gnu"]
You can run the build using netlify-cli:
netlify build
The site should build using cargo and the command we set up.
(build.command completed in 35.7s)
────────────────────────────────────────────────────────────────
2. Functions bundling
────────────────────────────────────────────────────────────────
Packaging Functions from functions directory:
- pokemon-api
(Functions bundling completed in 203ms)
────────────────────────────────────────────────────────────────
Netlify Build Complete
────────────────────────────────────────────────────────────────
(Netlify Build completed in 35.9s)
Once built, we can deploy to a preview using netlify deploy or to production:
netlify deploy --prod
Deploying to main site URL...
...
✔ Deploy is live!
and deploy your new serverless Rust function.