Playing Dog with the Cloud

2022/12/02

Categories: Cloud

No, that isn’t a typo. If “playing God” means behave as if all-powerful or supremely important, then “playing dog” is obviously the opposite, i.e., an action of little significance. Eh? Ehhh?

Anyway, this is a tale of two tiny learning experiences. I wanted to get some hands-on experience with Google Cloud Functions and fly.io, so I built xs.dog and excess.dog. Neither build-out took very long, but both were fun!

Puny pups and Google Cloud Functions

Yes, all this site does is show you a tiny dog in a cup. But there’s quite a bit going on!

When you go to xs.dog, a DNS lookup tells the browser what IP address to head over to. The A and AAAA records (for IPv4 and IPv6 respectively) for this were registered by me in Namecheap where I initially bought the domain. I leveraged Cloud Run domain mapping to figure out what IPs the domain should point to! With this, a managed certificate for HTTPS is automatically issued and renewed. Yet it took a bit of waiting before the certificate was provisioned and ready to go (~30 minutes).

Once you reach the right IP, something needs to pass back our puppy. That’s the code running as a 2nd gen Cloud Function (which gets backed by Cloud Run); I used the following NodeJS to get the job done:

'use strict';

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

exports.serveDog = function serveDog(req, res) {
  let file = storage.bucket('xs-dog-pictures').file('cup-dog.jpeg');
  let readStream = file.createReadStream();

  res.setHeader("content-type", "image/jpeg");
  readStream.pipe(res);
};

This code gets triggered by an HTTP GET request, and looks up and serves an image file from the GCS bucket I created to house it.

Flabby Fido and fly.io

In comparison, going from 0 to 100 in serving huge hounds with Fly was much quicker! This was partly because I had the code from the previous exercise already, but certificate provisioning was faster (~1-2 min). Their Node guide and custom domain guide were pretty much all I needed once I finished signing up for Fly. The code is practically the same:

const express = require("express");
const app = express();
const port = process.env.PORT || 3000;

app.get(["/"], (req, res) => {
    res.sendFile(__dirname + '/big-dog.jpg');
});

app.listen(port, () => console.log(`Here's a big boi!`));

I got this domain on GoDaddy so the DNS record updates were done there; other than that, the process remained the same as in the previous scenario.

Canine conclusion

I have more poking around to do with both platforms, but the ease of getting up and running on a basic web app bodes well for when I have tiny project ideas in the future. See you next time!

>> Home