Caddy your Serverless Functions

(Photo by Ryan De Hamer)

Use Caddy to bring your serverless functions under one "custom domain" roof, with automatic HTTPS.

Why host your own API gateway?

First a public service announcement:

Each of the major cloud providers have an API management service that works well with their functions-as-a-service product. You may prefer to use that instead of hosting your own server.

For my personal project I wanted to:

Configuring Caddy v2

Caddy is an open source web server that offers a streamlined configuration for automatic HTTPS (by handling the certificate renewal for you) and reverse proxying.

This configuration example shows how to hide https://us-central1-abc123.cloudfunctions.net/myCloudFunction beyond a custom domain and expose the function at https://myfunction.mydomain.com/add

https://myfunction.mydomain.com {
  tls myemail@gmail.com
  handle_path /add* {
    rewrite * /myCloudFunction{uri}
    reverse_proxy {
      header_up Host "us-central1-abc123.cloudfunctions.net"
      to https://us-central1-abc123.cloudfunctions.net
    }
  }
  log {
    output file /logs/myfunction.mydomain.com.log
   }
}

Here are the highlights:

If you're currently using an older version of Caddy (v1)

Caddy v2 was released in May 2020 and I recently went through the exercise of upgrading my Caddy v1 configuration and ran into a couple of challenges:

For reference, here was my original configuration from Caddy v1's:

https://myfunction.mydomain.com {
  tls myemail@gmail.com
  proxy /add https://us-central1-abc123.cloudfunctions.net/myCloudFunction
  errors /log/myfunction.mydomain.com.errors.log
  log / /log/myfunction.mydomain.com.log "{combined}" {
    rotate_size 100
    rotate_keep 10
  }
}

Wrapping up

As an engineer, hosting your own server with open source technology is a great way to stay sharp and learn about what's happening behind-the-scenes.