Skip to content

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:

  1. Install the kraft CLI tool and a container runtime engine, e.g. Docker.

  2. Clone the examples repository and cd into the examples/wazero-import-go/ directory:

Terminal window
git clone https://github.com/kraftcloud/examples
cd examples/wazero-import-go/

Make sure to log into Unikraft Cloud by setting your token and a metro close to you. We use fra0 (Frankfurt, πŸ‡©πŸ‡ͺ) in this guide:

Terminal window
# Set Unikraft Cloud access token
export UKC_TOKEN=token
# Set metro to Frankfurt, DE
export UKC_METRO=fra0

When done, invoke the following command to deploy this application on Unikraft Cloud:

Terminal window
kraft cloud deploy -p 443:8080 . /age-calculator 2000

The output shows the instance URL and other details:

Terminal window
[●] Deployed successfully!
β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ name: wazero-import-go-r4dx8
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ uuid: a763e1c3-bb38-475f-95b6-1e78d8ca74fc
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ state: running
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ url: https://cool-morning-camrrhsa.fra0.kraft.host
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ image: wazero-import-go@sha256:865700d358ffb2751888798ec8f302d23310b1fcf84f4d3f17f79fc25ff71153
β”œβ”€β”€β”€β”€β”€ boot time: 20.04 m
β”œβ”€β”€β”€β”€β”€β”€β”€β”€ memory: 512 MiB
β”œβ”€β”€β”€β”€β”€β”€β”€ service: cool-morning-camrrhsa
β”œβ”€β”€ private fqdn: wazero-import-go-r4dx8.internal
β”œβ”€β”€β”€β”€ private ip: 172.16.6.7
└────────── args: /age-calculator 2000

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:

Terminal window
curl https://cool-morning-camrrhsa.fra0.kraft.host
println >> 24
log_i32 >> 24

At any point in time, you can list information about the instance:

Terminal window
kraft cloud instance list
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME
wazero-import-go-r4dx8 cool-morning-camrrhsa.fra0.kraft.host running 1 minutes ag wazero-import-go@s... 512 MiB /age-calculator 2000 20040us

When done, you can remove the instance:

Terminal window
kraft cloud instance remove wazero-import-go-r4dx8

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 calculation
  • Kraftfile: the Unikraft Cloud specification
  • Dockerfile: the Docker-specified application filesystem

Learn More

Use the --help option for detailed information on using Unikraft Cloud:

Terminal window
kraft cloud --help

Or visit the CLI Reference.