Deploy a wasm function on Koyeb in a blast with Simplism.

Deploy a wasm function on Koyeb in a blast with Simplism.

Quick reminders:

  • Simplism is a highly lightweight application server that can serve WebAssembly plugins via HTTP as microservices (or even nanoservices) speedily and straightforwardly. Simplism is cloud provider-agnostic, allowing you to deploy it anywhere, from a simple Raspberry Pi Zero to Kubernetes. You can read the previous blog post for more details: "Simplism, the straightforward way for a Wasm FaaS on Clever Cloud."

  • Koyeb is a developer-friendly serverless platform to deploy apps globally. No-ops, servers, and infrastructure management.

The purpose of this blog post is to explain how easy it is to deploy and run a WebAssembly function with Simplism on Koyeb.

The Wasm function

The Simplism application server is capable of downloading a remote Wasm file before executing it, as follows:

simplism listen small-cow.wasm handle \
--http-port 8080 \
--wasm-url https://github.com/simplism-registry/small-cow/releases/download/v0.0.0/small-cow.wasm

In this example, the Wasm file is a release asset on GitHub, but you can serve the file with any HTTP server. The handle parameter represents the function's name in the wasm plugin to be executed.

👋 You can find all the source code on https://github.com/simplism-registry/small-cow

It's possible, too, to use Docker to serve the wasm file:

docker run \
-p 8080:8080 \
-v $(pwd):/app \
--rm k33g/simplism:0.0.7 \
/simplism listen ./app/small-cow.wasm handle \
--http-port 8080 \
--wasm-url https://github.com/simplism-registry/small-cow/releases/download/v0.0.0/small-cow.wasm

And you can query the wasm function like this:

curl http://localhost:8080 \
-d '👋 Hello World 🌍'

# output:
^__^
(oo)\_______
(__)\       )\/\
    ||----w |
    ||     ||
👋 Hello World 🌍

This is the source code of the function:

// main package
package main

import (
    "encoding/json"
    "strings"

    "github.com/extism/go-pdk"
)

// RequestData structure (from the request)
type RequestData struct {
    Body   string              `json:"body"`
    Header map[string][]string `json:"header"`
    Method string              `json:"method"`
    URI    string              `json:"uri"`
}

// ResponseData structure (for the response)
type ResponseData struct {
    Body   string              `json:"body"`
    Header map[string][]string `json:"header"`
    Code   int                 `json:"code"`
}

//export handle
func handle() {
    // read request data from the memory
    input := pdk.Input()

    var requestData RequestData
    json.Unmarshal(input, &requestData)

    cow := []string{
        `^__^`,
        `(oo)\_______`,
        `(__)\       )\/\`,
        `    ||----w |`,
        `    ||     ||`,
        requestData.Body,
    }

    responseData := ResponseData{
        Body:   strings.Join(cow, "\n"),
        Header: map[string][]string{"Content-Type": {"text/plain; charset=utf-8"}},
        Code:   200,
    }
    // response to Json string
    jsonResponse, _ := json.Marshal(responseData)

    // copy output to host memory
    mem := pdk.AllocateBytes(jsonResponse)
    pdk.OutputMemory(mem)

}

func main() {}

👋 I "gitpodified" the project https://github.com/simplism-registry/small-cow, so if you open it with Gitpod, you will get an environment and all the necessary tools to build and run the wasm function. But to deploy the wasm function, you only need to be able to get the pre-compiled wasm file from https://github.com/simplism-registry/small-cow/releases/download/v0.0.0/small-cow.wasm.

Deployment of the function

Preparation

The workflow will be the following:

  • Koyeb will clone a GitHub repository containing a Dockerfile.

  • The Dockerfile will use the Simplism Docker image.

  • The build container will download the wasm file from the GitHub release asset of the function repository.

  • Then, the container will serve the wasm file.

This is the content of the Dockerfile:

FROM k33g/simplism:0.0.7

EXPOSE 8080
CMD [ "/simplism",                  \
      "listen",                     \
      "small-cow.wasm",             \
      "handle",                     \
      "--http-port",                \
      "8080",                       \
      "--wasm-url",                 \
      "https://github.com/simplism-registry/small-cow/releases/download/v0.0.0/small-cow.wasm" \
    ]

The repository with the Dockerfile is here: https://github.com/k33g/small-cow

Deployment to Koyeb

Prerequisite: you need to create an account on https://www.koyeb.com/. There is a free plan offering you one single instance of a service.

Once you are in the administration panel:

Select "Create Web Service":

Choose "GitHub repository" as the deployment method:

Select the GitHub repository with the Dockerfile:

Then choose the "Builder method":

Choose the "Web Service" service type:

Then, collapse the "Advanced panel" and change the value of the HTTP port to 8080:

Change the name of the application and click on the "Deploy" button:

Now, wait for a minute:

🎉 The wasm function is deployed:

It's time to call the function:

curl https://small-cow-bots-garden.koyeb.app \
-d '👋 Hello World 🌍'

👋 And here it is, it's finished; it's simple and fast, thanks to Koyeb and the efficiency of the Simplism application server (the Docker image weighs less than 9Mb).

At every change (and commit) of the GitHub repository (for example to change the release version to download), the wasm function will be automatically redeployed.

In future blog posts, we will see other options for deploying and operating Simplism.