Wasm Builders 🧱

Cover image for Deploy a Sat Serverless function with to Fly.io
Philippe Charrière
Philippe Charrière

Posted on

Deploy a Sat Serverless function with to Fly.io

Connor Hicks published a nice blog post on how to write a wasm GoLang function with Sat: "A simple data hashing serverless function using Sat".

Sat has no dependencies, and can run in a tiny Docker container. So it was an opportunity to deploy it quickly on Fly.io. Fly.io is probably the most straightforward service to deploy containers in the cloud.

So let me walk you through how to deploy Connor's function with Sat on Fly.io.

Prerequisites

You will need:

  • Docker (and a Docker Hub account)
  • The Subo CLI
  • The Fly.io CLI (and a Fly.io account)

Fly.io

Install the Fly.io CLI

Install the Fly.io CLI: See 👀 https://fly.io/docs/getting-started/installing-flyctl/

brew install superfly/tap/flyctl
Enter fullscreen mode Exit fullscreen mode

Create an account on Fly.io

You need an account on Fly.io

flyctl auth signup
# you need a credit card to create an account (but there is a free plan)
Enter fullscreen mode Exit fullscreen mode

Create a Fly.io token

You need to create a Fly.io token (after the sign up):

Suborbital

Install the Subo CLI

You need to install the Subo CLI: See 👀 https://github.com/suborbital/subo

brew tap suborbital/subo
brew install subo
Enter fullscreen mode Exit fullscreen mode

Create a Runnable project, and build the wasm function

I reproduced the steps of "A simple data hashing serverless function using Sat", then my project looks like this:

.
└── sha256
   ├── go.mod
   ├── go.sum
   ├── main.go
   └── sha256.wasm
Enter fullscreen mode Exit fullscreen mode

I used the subo build . command to build the wasm module.

Prepare the project for deployment

You will need:

  • a Dockerfile
  • a fly.toml configuration file

Dockerfile

We will simply use the suborbital/sat image:

FROM suborbital/sat:latest

COPY ./sha256.wasm .

ENTRYPOINT [ "sat", "./sha256.wasm" ]
Enter fullscreen mode Exit fullscreen mode

Fly.io configuration

You need to create a fly.toml file at the root of the Runnable project sha256 (it's the application configuration file: https://fly.io/docs/reference/configuration/)

kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []

  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"
Enter fullscreen mode Exit fullscreen mode

Now, your project should look like that:

.
└── sha256
   ├── Dockerfile
   ├── fly.toml
   ├── go.mod
   ├── go.sum
   ├── main.go
   └── sha256.wasm
Enter fullscreen mode Exit fullscreen mode

Build and push the Docker image to the Docker Hub

docker_handle="your_docker_hub_handle"
docker_pwd="your_docker_hub_password"
docker login -u ${docker_handle} -p ${docker_pwd}

cd sha256
app_name="sha256-demo"
tag="0.0.0"
docker build -t ${app_name} . 
docker tag ${app_name} ${docker_handle}/${app_name}:${tag}
docker image ls
docker push ${docker_handle}/${app_name}:${tag}
Enter fullscreen mode Exit fullscreen mode

Deployment of the function

If it's the first deployment, you have to create a new application on Fly.io with the flyctl:

Create the Fly.io application

# Create the application, only at the first deployment
flyctl apps create ${app_name} --json
Enter fullscreen mode Exit fullscreen mode

And finally, deploy the Fly.io application

# 🖐️ Don't forget to set FLY_ACCESS_TOKEN
flyctl deploy \
  --app ${app_name} \
  --image ${docker_handle}/${app_name}:${tag} \
  --env SAT_HTTP_PORT=8080 \
  --verbose --json

Enter fullscreen mode Exit fullscreen mode

Wait for a moment, then, when the deployment is finished, call the function:

curl -d '👋 Hello World 🌍' https://sha256-demo.fly.dev
Enter fullscreen mode Exit fullscreen mode

You should get something like: uC-S3Jtx3j3NVm7KF8jv_Zus9zPmWCd3g_lROeLtB-Y

The ease of deployment of Fly.io is surprising. Now I can easily create hooks, bots and functions with Sat and then deploy them in the blink of an eye.

One more thing: it's that easy to deploy an Atmo project. I let you discover the code here: https://github.com/wasm-builders/atmo-on-fly

Header Image by WikiImages from Pixabay

Top comments (5)

Collapse
 
hb9cwp profile image
Rolf Sommerhalder

Thanks for your great post! I took it as an example to learn about Fly.io and WebAssembly with Go.
After a first iteration over at community.fly.io [1], I hope the friendly and very active community there will help me to bake your manual steps further into a Dockerfile so that a simple 'flyctl launch' builds remotely on Fly and runs it.
[1] WASM runtime|platform? community.fly.io/t/wasm-runtime-pl...

Collapse
 
k33g_org profile image
Philippe Charrière Author

I think it's possible, but complicated because you will need the subo CLI to build the Runnable module. And the subo CLI uses Docker too, so you will do Docker in Docker.
But you can probably avoid this by using the --native flag of the subo CLI.

For the building step in the Dockerfile, you will need Go + TinyGo + the subo CLI.

Collapse
 
anara profile image
Silvana

Phillippe this set up is only possible in linux machine ?

Collapse
 
k33g_org profile image
Philippe Charrière Author

You can do the same with MacOS

On Windows, I think something should be possible with Docker ou you can use a VM like Multipass

Collapse
 
k33g_org profile image
Philippe Charrière Author

Most of the time, I use gitpod.io/, so it works from any OS