This blog post is one ofa series looking at using Docker Actions in OpenWhisk to support extra runtimes.
Let’s look at writing serverless functions for OpenWhisk using Rust.
Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.
Rust has been growing in popularity since it launched in 2010. Rust is a popular language for writing microservices due to the focus on the attention to safety and strong concurrency support.
None of the major serverless platform natively support Rust at the moment. OpenWhisk does not include this as a default runtime. However, recent updates to OpenWhisk provide a path for writing serverless functions with Rust.
Let’s re-write the example from the previous post in Rust and see how to get it running using this new approach…
Have you seen this post explaining how Docker-based Actions work? This post assumes you have already read that first.
Rust Language Actions
Rust has a build system that supports creating static binaries. These binaries contain the application source code and dependent libraries.
Using the same approach as the Go-based example, bundling this binary into a zip file allows us to overwrite the runtime stub prior to invocation.
Runtime binaries will be executed by the Python-based invoker for each invocation. Request parameters will be passed as a JSON string using the first command-line argument. The invoker expects the Action result to be written to standard output as a JSON string.
Action Source Code
Here’s a simple Rust function that returns a greeting string from an input parameter. It parses the JSON string provided on the command-line to look for a name parameter. If this isn’t present, it defaults to stranger. It returns a JSON object with the greeting string (msg) by writing to the console.
Before we can deploy this binary to OpenWhisk, it must be compiled for the platform architecture.
Rust’s compiler uses LLVM under the covers, making it possible to generate machine code for different architectures. Cross-compiling for different platforms requires having the correct compiler, linker and libraries for that architecture installed.