-- Leo's gemini proxy

-- Connecting to gmi.runtimeterror.dev:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini;lang=en-US

💻 [runtimeterror $]

2021-08-20 ~ 2022-02-03

Free serverless URL shortener on Google Cloud Run


Intro

I've been using short.io [1] with a custom domain to keep track of and share messy links for a few months now. That approach has worked very well, but it's also seriously overkill for my needs. I don't need (nor want) tracking metrics to know anything about when those links get clicked, and short.io doesn't provide an easy way to turn that off. I was casually looking for a lighter self-hosted alternative today when I stumbled upon a *serverless* alternative: **sheets-url-shortener [2]**. This uses Google Cloud Run [3] to run an ultralight application container which receives an incoming web request, looks for the path in a Google Sheet, and redirects the client to the appropriate URL. It supports connecting with a custom domain, and should run happily within the Cloud Run Free Tier limits [4].

[1] short.io

[2] sheets-url-shortener

[3] Google Cloud Run

[4] Cloud Run Free Tier limits

The Github instructions were pretty straight-forward but I did have to fumble through a few additional steps to get everything up and running. Here we go:

Shortcut mapping

Since the setup uses a simple Google Sheets document to map the shortcuts to the original long-form URLs, I started by going to https://sheets.new [5] to create a new Sheet. I then just copied in the shortcuts and URLs I was already using in short.io. By the way, I learned on a previous attempt that this solution only works with lowercase shortcuts so I made sure to convert my `MixedCase` ones as I went.

Image: Creating a new sheet

[5] https://sheets.new

I then made a note of the Sheet ID from the URL; that's the bit that looks like `1SMeoyesCaGHRlYdGj9VyqD-qhXtab1jrcgHZ0irvNDs`. That will be needed later on.

Create a new GCP project

I created a new project in my GCP account by going to https://console.cloud.google.com/projectcreate [6] and entering a descriptive name.

Image: Creating a new GCP project

[6] https://console.cloud.google.com/projectcreate

Deploy to GCP

At this point, I was ready to actually kick off the deployment. Ahmet made this part exceptionally easy: just hit the **Run on Google Cloud** button from the Github project page [7]. That opens up a Google Cloud Shell instance which prompts for authorization before it starts the deployment script.

Image: Open in Cloud Shell prompt

[7] Github project page

Image: Authorize Cloud Shell prompt

The script prompted me to select a project and a region, and then asked for the Sheet ID that I copied earlier.

Image: Cloud Shell deployment

Grant access to the Sheet

In order for the Cloud Run service to be able to see the URL mappings in the Sheet I needed to share the Sheet with the service account. That service account is found by going to https://console.cloud.google.com/run [8], clicking on the new `sheets-url-shortener` service, and then viewing the **Permissions** tab. I'm interested in the one that's `############-computer@developer.gserviceaccount.com`.

Image: Finding the service account

[8] https://console.cloud.google.com/run

I then went back to the Sheet, hit the big **Share** button at the top, and shared the Sheet to the service account with *Viewer* access.

Image: Sharing to the service account

Quick test

Back in GCP land, the details page for the `sheets-url-shortener` Cloud Run service shows a gross-looking URL near the top: `https://sheets-url-shortener-vrw7x6wdzq-uc.a.run.app`. That doesn't do much for *shortening* my links, but it'll do just fine for a quick test. First, I pointed my browser straight to that listed URL:

Image: Testing the web server

This at least tells me that the web server portion is working. Now to see if I can redirect to my project car posts on Polywork [9]:

Image: Testing a redirect

[9] project car posts on Polywork

Hmm, not quite. Luckily the error tells me exactly what I need to do...

Enable Sheets API

I just needed to visit `https://console.developers.google.com/apis/api/sheets.googleapis.com/overview?project=############` to enable the Google Sheets API.

Image: Enabling Sheets API

Once that's done, I can try my redirect again - and, after a brief moment, it successfully sends me on to Polywork!

Image: Successful redirect

Link custom domain

The whole point of this project is to *shorten* URLs, but I haven't done that yet. I'll want to link in my `go.bowdre.net` domain to use that in place of the rather unwieldy `https://sheets-url-shortener-somestring-uc.a.run.app`. I do that by going back to the Cloud Run console [10] and selecting the option at the top to **Manage Custom Domains**.

Image: Manage custom domains

[10] Cloud Run console

I can then use the **Add Mapping** button, select my `sheets-url-shortener` service, choose one of my verified domains (which I *think* are already verified since they're registered through Google Domains with the same account), and then specify the desired subdomain.

Image: Adding a domain mapping

The wizard then tells me exactly what record I need to create/update with my domain host:

Image: CNAME details

It took a while for the domain mapping to go live once I've updated the record.

Image: Processing mapping...

Final tests

Once it did finally update, I was able to hit `https://go.bowdre.net` to get the error/landing page, complete with a valid SSL cert:

Image: Successful error!

Outro

I'm very pleased with how this quick little project turned out. Managing my shortened links with a Google Sheet is quite convenient, and I really like the complete lack of tracking or analytics. Plus I'm a sucker for an excuse to use a cloud technology I haven't played a lot with yet.

And now I can hand out handy-dandy short links!

| Link                            | Description                                               |
|---------------------------------|-----------------------------------------------------------|
| go.bowdre.net/coso [11]         | Follow me on CounterSocial                                |
| go.bowdre.net/conedoge [12]     | 2014 Subaru BRZ autocross videos                          |
| go.bowdre.net/cooltechshit [13] | A collection of cool tech shit (references and resources) |
| go.bowdre.net/stuffiuse [14]    | Things that I use (and think you should use too)          |
| go.bowdre.net/shorterer [15]    | This post!                                                |

[11] go.bowdre.net/coso

[12] go.bowdre.net/conedoge

[13] go.bowdre.net/cooltechshit

[14] go.bowdre.net/stuffiuse

[15] go.bowdre.net/shorterer




---


📧 Reply by email



Related articles


Blocking AI Crawlers

Displaying Data from a Tempest Weather Station on a Static Site

Deploying a Hugo Site to Neocities with GitHub Actions

---


Home

This page on the big web

-- Response ended

-- Page fetched on Fri May 10 20:33:13 2024