Run a Wasm (Wazero) App on Go
This example is derived from Wazeroβs βimport goβ example and shows how to define, import and call a wasm blob from Go and run it on Unikraft Cloud. To run this it, follow these steps:
-
Install the
kraft
CLI tool and a container runtime engine, e.g. Docker. -
Clone the
examples
repository andcd
into theexamples/wazero-import-go/
directory:
Make sure to log into Unikraft Cloud by setting your token and a metro close to you.
We use fra0
(Frankfurt, π©πͺ) in this guide:
When done, invoke the following command to deploy this application on Unikraft Cloud:
The output shows the instance URL and other details:
In this case, the instance name is wazero-import-go-r4dx8
and the URL is https://cool-morning-camrrhsa.fra0.kraft.host
.
They are different for each run.
Use curl
to query the Unikraft Cloud instance of the Go/wazero server:
At any point in time, you can list information about the instance:
When done, you can remove the instance:
Background
WebAssembly has neither a mechanism to get the current year, nor one to print to the console, so we define these in Go.
Similar to Go, WebAssembly functions are namespaced into modules instead of packages. Just like Go, only exported
functions can be imported into another module. age-calculator.go
shows how to
export functions using HostModuleBuilder and how a
WebAssembly module defined in its text format
imports it. This only uses the text format for demonstration purposes, to show you whatβs going on. It is likely, you
will use another language to compile a Wasm (WebAssembly Module) binary, such as TinyGo. Regardless of how wasm is
produced, the export/import mechanics are the same!
WebAssembly System Interface (WASI)
This uses an ad-hoc Go-defined function to print to the console. There is an emerging specification to standardize system calls (similar to Goβs x/sys) called WebAssembly System Interface (WASI). While this is not yet a W3C standard, wazero includes a wasi package.
Customize your Application
To customize the application, update the files in the repository, listed below:
agecalculator.go
: The Go web server that calls the WASM/Wazero blog for the age calculationKraftfile
: the Unikraft Cloud specificationDockerfile
: the Docker-specified application filesystem
Learn More
Use the --help
option for detailed information on using Unikraft Cloud:
Or visit the CLI Reference.