Creating a WASM Function in Rust.
Web Assembly (WASM) support is a first-class feature in Rust, making Rust an excellent language to write WASM functions for Tarmac.
This guide will walk users through creating a WASM function for Tarmac in the Rust language. This walkthrough assumes there is some familiarity with the Rust tooling and language to start.
We will need to begin with a new project folder, creating the src/
directory. Within that directory, we will make our main.rs
file.
Tarmac internally uses a Web Assembly Procedure Calls (waPC) runtime, which means all WASM functions running within Tarmac must import and use a waPC compliant library.
Along with the waPC imports, you should also see a wapc_init()
function created. This function is the primary entry point for Tarmac execution. We will register our handler function for Tarmac to execute using the register_function()
function within this function.
In the example above, we have registered the handler()
function. When Tarmac receives an HTTP POST request for this WASM function, it will execute the handler function as defined.
With our handler function now registered, we must create a basic version of this handler for Tarmac to call.
As we can see from the example above, the handler input a slice of 8-bit unsigned integers, which is the raw HTTP payload. And a return value of CallResult.
Now that we have the basic structure of our WASM function created, we can start adding logic to the function and process our request.
One of the unique benefits of Tarmac is the ability for WASM functions to perform host callbacks to the Tarmac service itself. These Host Callbacks give users the ability to execute common framework code provided to the WASM function by Tarmac. These common framework functions can include storing data within a database, calling a remote API, or logging data.
For our example, we will use the Host Callbacks to create a Trace log entry.
For a full list of Host Callbacks checkout the Callbacks documentation.
We can add our logic to the example, which in this case will just return the input payload.
For quick reference, the below code is the full WASM function from this example.
Now that our function is ready, we must compile our Rust code into a .wasm
file. To do this, we will need to create our Cargo manifest and build the project.
Within the Cargo.toml
file, we must specify the different packages used in our WASM function.
With our manifest defined, we can now build our module.
After the code build completes, we will copy the .wasm
file into a directory Tarmac can use to run.
We are now ready to run our WASM function via Tarmac. To make this process easier, we will be using Docker to execute Tarmac. It is not necessary to use Docker with Tarmac as it can run outside of Docker as well.
In the above command, we pass two environment variables to the container using the -e flag. These environment variables will tell Tarmac to use HTTP rather than HTTPS, which is the default. For additional configuration options, check out the Configuration documentation.
With Tarmac now running, we can access our WASM function using any HTTP Client such as curl
.
Developers can use this guide to get started with WASM functions and using Tarmac. Some of the information in this guide is subject to change as WASM advances. However, the concepts should stay pretty consistent.