A serverless project is going to be set up the same as a regular cargo binary crate, so we can use cargo new or cargo init as usual
cargo new serverless-intro-netlify
two dependencies are required for the project: tokio and lambda_http.
tokio is an async runtime that will wrap our entire function, allowing us to use async functions.
lambda_http is one of a series of lambda crates hosted in the aws-labs github organization. These crates include the runtime we need for Rust to work as well as a selection of useful types and functions that make it easier to build lambda functions and extensions.
This may seem odd, because we’re working with Netlify Functions, not AWS Lambda, but Netlify Functions is a higher level service that runs on top of AWS Lambda, so the same tools work.
❯ cargo add lambda_http tokio -F tokio/macros -F lambda_http/apigw_rest
Updating crates.io index
Adding lambda_http v0.8.1 to dependencies.
Features:
+ alb
+ apigw_http
+ apigw_rest
+ apigw_websockets
Adding tokio v1.29.1 to dependencies.
Features:
+ macros
+ tokio-macros
- bytes
- fs
- full
- io-std
- io-util
- libc
- mio
- net
- num_cpus
- parking_lot
- process
- rt
- rt-multi-thread
- signal
- signal-hook-registry
- socket2
- stats
- sync
- test-util
- time
- tracing
- windows-sys
Updating crates.io index
Our Cargo.toml now looks like this:
[package]
name = "serverless-intro-netlify"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lambda_http = { version = "0.8.1", features = ["apigw_rest"] }
tokio = { version = "1.29.1", features = ["macros"] }
The tokio::main macro
We need the macros feature on the tokio crate because we’re going to use the tokio::main attribute macro to bootstrap the async runtime for us.
The tokio::main macro will generate the code that bootstraps the runtime and creates a new main function that calls our async “main” function.
#[tokio::main]
async fn main() {
println!("Hello, world!");
}
This is important because Rust doesn’t actually allow us to have an async main function, so tokio taking our async main function and calling it from the real, generated main function is how we’re getting around that and making it look like writing async main is ok.
lambda_http apigw_rest
Netlify Functions are a pretty restricted runtime environment compared to AWS Lambda. There are a great many ways to call through AWS services and trigger a lambda on AWS, and Netlify only uses one of those calling conventions.
This calling convention is called “API Gateway”, which is an AWS service. This is where the name of the apigw_rest feature comes from: “API Gateway Rest”.
If you’re interested in what other calling conventions there are, they are listed in the aws-lambda-rust-runtime README. We won’t need the others for this workshop though.
We’ll also disable the code for the other conventions since we won’t be using it by disabling the default-features on the lambda_http crate.
lambda_http = { version = "0.8.1", default-features = false, features = [
"apigw_rest",
] }
In the next lesson we’ll build out an actual lambda function.