Aniara

Blog

Deploying an app with Fly.io

This is how I generally deploy web applications with Fly.io. Typically simple hobby apps written as Go programs. If we have a program serving HTTP on $PORT then Fly.io will take care of building an image, deploying that image, routing traffic, HTTPS and so on.

Provision the application

flyctl launch <NAME> --no-deploy
flyctl deploy --local-only

Generally the default fly.toml generated by flyctl works out of box for me. The webserver hosting this blog has the following config:

app = "aniara-blog"
primary_region = "arn"
kill_signal = "SIGINT"
kill_timeout = 5

[build]
  builder = "paketobuildpacks/builder:base"
  buildpacks = ["gcr.io/paketo-buildpacks/go"]

[env]
  PORT = "8080"

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

[[vm]]
  cpu_kind = "shared"
  cpus = 1
  memory_mb = 256

If your application is slighly more complicated, for example placing main.go in some subdirectory, or needs some files copied into the image, that can be configured:

[build]
  builder = "paketobuildpacks/builder:base"
  buildpacks = ["gcr.io/paketo-buildpacks/go"]
  [build.args]
    BP_KEEP_FILES = "./static"
    BP_GO_TARGETS = "./cmd/server"

Custom domain

I don’t do anything fancy with my domain, so a simple CNAME record is enough.

Host name Type Data
blog.aniara.dev CNAME aniara-blog.fly.dev.
www.blog.aniara.dev CNAME blog.aniara.dev.

Add the custom domains to the certificates the app is using:

flyctl certs add blog.aniara.dev
flyctl certs add www.blog.aniara.dev

Now the custom domain should be working in your browser, with automatic HTTPS.

Troubleshooting

Test if DNS resolves your custom domain to the <NAME>.fly.dev that Fly.io typically hosts the app at. For example:

dig blog.aniara.dev +short
dig www.blog.aniara.dev +short

Test if certificates are being written as expected when using HTTPS:

echo "Q" | openssl s_client -connect aniara-blog.fly.dev:443 -showcerts
echo "Q" | openssl s_client -connect blog.aniara.dev:443 -showcerts