Zudoku
Guides

Wazero

This example comes 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, for example Docker.

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

Code(bash)
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. This guide uses fra (Frankfurt, ๐Ÿ‡ฉ๐Ÿ‡ช):

Code(bash)
# Set Unikraft Cloud access token export UKC_TOKEN=token # Set metro to Frankfurt, DE export UKC_METRO=fra

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

Code(bash)
kraft cloud deploy -p 443:8080 . /age-calculator 2000

The output shows the instance address and other details:

Code(ansi)
[โ—] Deployed successfully! โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ name: wazero-import-go-r4dx8 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ uuid: a763e1c3-bb38-475f-95b6-1e78d8ca74fc โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ state: running โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ url: https://cool-morning-camrrhsa.fra.unikraft.app โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 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 address is https://cool-morning-camrrhsa.fra.unikraft.app. They're different for each run.

Use curl to query the Unikraft Cloud instance of the Go/wazero server:

Code(bash)
curl https://cool-morning-camrrhsa.fra.unikraft.app
Code(text)
println >> 24 log_i32 >> 24

You can list information about the instance by running:

Code(bash)
kraft cloud instance list
Code(text)
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME wazero-import-go-r4dx8 cool-morning-camrrhsa.fra.unikraft.app running 1 minutes ag wazero-import-go@s... 512 MiB /age-calculator 2000 20040us

When done, you can remove the instance:

Code(bash)
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 this example defines these in Go. Like Go, WebAssembly functions are namespaced into modules instead of packages. With Go only exported functions can import 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's likely, you will use another language to compile a Wasm (WebAssembly Module) binary, such as TinyGo. Regardless of how wasm produces, the export/import mechanics are the same!

Using WASI

WebAssembly System Interface (WASI) is a modular system interface for WebAssembly. This uses an ad-hoc Go-defined function to print to the console. An emerging specification standardizes system calls (like Go's x/sys) called WebAssembly System Interface (WASI). While this isn't yet a W3C standard, wazero includes a wasi package.

Customize your app

To customize the app, 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 app filesystem

Learn more

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

Code(bash)
kraft cloud --help

Or visit the CLI Reference.

Last modified on