Lesson Details

To deploy our serverless function, we’ll need a Netlify account and a new netlify site. Install the netlify CLI, which requires nodejs installed, and run netlify sites:create.

netlify sites:create

In .netlify/state.json, there will be a siteId which is how netlify will be able to tell which site to deploy to.

{
	"siteId": "97ca1ab4-08d7-4fa5-8a2c-e1add2a45f00"
}

We’ll have to build and put our function in the right place before deploying. I also tend to use a placeholder index.html file so that there is a “site” to deploy.

In a new directory, create site/index.html and drop some placeholder html with an a tag that will be able to trigger our function, which will end up at .netlify/functions/hello.

<!DOCTYPE html>
<html class="no-js" lang="">
  <head>
    <meta charset="utf-8" />
    <title></title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <meta property="og:title" content="Rust Adventure: Intro to Serverless!" />
  </head>

  <body>
    <a href="/.netlify/functions/hello">test function</a>
  </body>
</html>

netlify.toml

Netlify uses a configuration file called netlify.toml in the root of our project to configure where the site is, and where the functions live.

[build]
publish = "site/"

Building the function

cargo lambda enables us to build our function, which gets placed in the target/lambda/ directory.

We then need to take that binary and place it somewhere the netlify CLI will find it. By default this is in netlify/functions/.

cargo lambda build

We’ll use the debug build, since as of recording Netlify currently has a bug in its binary detection that could miss a release binary.

copy the bootstrap binary from the target/lambda/serverless-intro-netlify directory into the netlify/functions directory.

mkdir netlify
mkdir netlify/functions
cp target/lambda/serverless-intro-netlify/bootstrap netlify/functions/hello

Run netlify deploy to deploy a preview of the site.

❯ netlify deploy
Deploy path:        /rust-adventure/serverless-intro-netlify/site
Functions path:     /rust-adventure/serverless-intro-netlify/netlify/functions
Configuration path: /rust-adventure/serverless-intro-netlify/netlify.toml
Deploying to draft URL...
✔ No cached functions were found
✔ Finished hashing 2 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>/deploys/64cf03fad144560f1ff62214
Function logs:     https://app.netlify.com/sites/<site-name>/functions?scope=deploy:64cf03fad144560f1ff62214
Website draft URL: https://64cf03fad144560f1ff62214--<site-name>.netlify.app

If everything looks good on your draft URL, deploy it to your main site URL with the --prod flag.
netlify deploy --prod

The key here is that 1 functions were deployed. Use the Website draft URL to visit the website, and the Function logs URL to see the function logs.

Visit the netlify site at the function’s URL: /.netlify/functions/hello or send a curl request. You should, after a short delay, see function logs detailing the runtime, memory usage, and init duration. If you left any dbg! expressions, you’ll see that output as well.

Aug 5, 07:38:48 PM: INIT_START Runtime Version: provided:al2.v20	Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:e448fca582d6ef7f5f1faf3cae4b2bb6521010ebae50e60019812234a4c0d549
...
Aug 5, 07:38:48 PM: 43126b2d Duration: 6.07 ms	Memory Usage: 21 MB	Init Duration: 51.86 ms

The init duration is how long it took to cold start the function, which only happens once in awhile.

The duration is the runtime of the function itself. Make another few requests and the duration will drop, now that it’s bootstrapped and we’re only sending the event through to our function handler instead of booting the whole function up from scratch.

Aug 5, 07:40:02 PM: 127f1e5c Duration: 3.04 ms	Memory Usage: 22 MB
Aug 5, 07:40:21 PM: ca5493c9 Duration: 2.04 ms	Memory Usage: 22 MB
Aug 5, 07:40:37 PM: 8f0ed24c Duration: 2.20 ms	Memory Usage: 22 MB

The memory usage stays the same, since we aren’t really doing anything that requires additional memory.

If you want the functions folder to stay around after committing to git, create an empty file called netlify/functions/.gitkeep.