Thoughts by Bruno Bernardino

Handle URL Path Redirects “serverlessly”

Really, using AWS Lambda and API Gateway

December, 2016


I know, serverless isn’t really serverless! There’s a server somewhere!

Now that’s out of the way, let me tell you about the time when I wanted to stop paying for servers, as a limitation experiment (which I’ve been loving).

That one tiny little thing

After I was able to move all apps and sites to GitHub’s static hosting, there was one tiny little thing that I couldn’t have functionality-parity on: some custom, logic-based path URL redirects for my personal website.

See, I used to host my own “files to share” with something I built and Open Sourced, called filebox.js.

Once I moved from that to Dropbox and then Google Drive (and now Tresorit, because of privacy awareness), I wanted to make sure the redirects I had from those files to Dropbox got redirected to the new Google Drive places (think things like sub.domain.com/randomgibberish.jpg getting redirected to drive.google.com shared links).

The path of lowest resistance

I started by removing some redirects for files I was pretty sure few people would have access to or care about. A lot of work out of the way!

Unfortunately, some I knew were shared in forums where people expected to be able to access those same images and such, so I wanted to keep those working.

Enter AWS Lambda and API Gateway!

After researching for a bit, it seemed to me like it would be an easy feat:

  1. Setup AWS Lambda code with logic for redirects
  2. Setup API Gateway (screenshots below)
  3. Get my subdomain “redirecting” to the API Gateway domain (DNS, CNAME)

All good, except I couldn’t find any good information about how to just send back a dynamic “Location” header, and that was where I spent most of the time and almost gave up!

Turns out to be really easy (after you know it), they key was the Mapping value namespace of “integration.response.body” that I couldn’t find easily for the “Location” Response header.

Here are the API Gateway screenshots for how the whole setup works, as that’s easier than explaining all the process of clicking through things:

Overview

Overview

Method Request

Method Request

Integration Request

Integration Request

Integration Response

Integration Response

Method Response

Method Response

The most important parts are the setup in the Method Response for the Headers and the Integration Response for the Header Mappings.

It’s not impossible there are some leftovers of me testing things, and this not being optimized, but it’s costing me $0.01 per month, so I won’t think about it much more.

I hope it helps someone else!


Bruno Bernardino

Written by Bruno Bernardino.
Thoughts can change, disappear, or simply be observed.

Go back to all thoughts.