mirror of
https://github.com/jbowdre/runtimeterror.git
synced 2025-02-27 12:22:13 +00:00
90 lines
6.8 KiB
Markdown
90 lines
6.8 KiB
Markdown
---
|
|
series: Projects
|
|
date: "2021-08-20T00:00:00Z"
|
|
lastmod: 2022-02-03
|
|
usePageBundles: true
|
|
tags:
|
|
- gcp
|
|
- cloud
|
|
- serverless
|
|
title: Free serverless URL shortener on Google Cloud Run
|
|
---
|
|
### Intro
|
|
I've been using [short.io](https://short.io) 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](https://github.com/ahmetb/sheets-url-shortener)**. This uses [Google Cloud Run](https://cloud.google.com/run/) 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](https://cloud.google.com/run/pricing).
|
|
|
|
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](https://sheets.new) 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.
|
|

|
|
|
|
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](https://console.cloud.google.com/projectcreate) and entering a descriptive name.
|
|

|
|
|
|
### 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](https://github.com/ahmetb/sheets-url-shortener#setup). That opens up a Google Cloud Shell instance which prompts for authorization before it starts the deployment script.
|
|

|
|
|
|

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

|
|
|
|
### 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](https://console.cloud.google.com/run), 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`.
|
|

|
|
|
|
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.
|
|

|
|
|
|
### 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:
|
|

|
|
|
|
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](https://john.bowdre.net/?badges%5B%5D=Car+Nerd):
|
|

|
|
|
|
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.
|
|

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

|
|
|
|
### 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-vrw7x6wdzq-uc.a.run.app`. I do that by going back to the [Cloud Run console](https://console.cloud.google.com/run) and selecting the option at the top to **Manage Custom Domains**.
|
|

|
|
|
|
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.
|
|

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

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

|
|
|
|
### 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:
|
|

|
|
|
|
And testing [go.bowdre.net/ghia](https://go.bowdre.net/ghia) works as well!
|
|
|
|
### 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](https://go.bowdre.net/coso) | Follow me on CounterSocial |
|
|
| [go.bowdre.net/conedoge](https://go.bowdre.net/conedoge) | 2014 Subaru BRZ autocross videos |
|
|
| [go.bowdre.net/cooltechshit](https://go.bowdre.net/cooltechshit) | A collection of cool tech shit (references and resources) |
|
|
| [go.bowdre.net/stuffiuse](https://go.bowdre.net/stuffiuse) | Things that I use (and think you should use too) |
|
|
| [go.bowdre.net/shorterer](https://go.bowdre.net/shorterer) | This post! |
|
|
|