6 Easy Steps to Redirect from Subdomains to URLs using Amazon’s Route 53 and S3

Originally published at: 6 Easy Steps to Redirect from Subdomains to URLs using Amazon’s Route 53 and S3 - Skycrafters

Recently I created a Calendly to call my own. If you don’t know what it is, it’s ok. I didn’t either. My own summary is: It’s a service, which is free for personal use, that integrates to your personal/work calendar, like Outlook or G Apps, and exposes an easy-to-use portal where people can request some time with you, based on your own availability and rules, such as “I don’t want meetings longer than 30 minutes and also don’t book on Fridays – because everybody knows this is beer day”.

It generates a https://calendly.com/USERNAME URL for the user, which is fine, but I wanted to make it more personal. I wanted to use my own domain. Since I already manage my raphabot.com domain over AWS Route 53, I knew it would be as simple as creating a CNAME entry to redirect to the right place. Or so I thought.

CNAME

Despite CNAME not being something exclusive to Route 53, let me share their description for it:

A CNAME record can redirect DNS queries to any DNS record. For example, you can create a CNAME record that redirects queries from acme.example.com to zenith.example.com or to acme.example.org. You don’t need to use Route 53 as the DNS service for the domain that you’re redirecting queries to.

The bold part was my own personal touch to highlight how CNAME works. It can easily redirect, but to other DNS records only! So subdomain.raphabot.com can redirect to calendly.com, but it cannot redirect to calendly.com/USERNAME. Oh no! I really don’t want to write HTML and JS code just to redirect users from one place to the other…

S3 Web Hosting

There is hope, though. While I was checking S3’s documentation for “Hosting a static website”, I realized there was a field for “Configuring a webpage redirect”! According to the documentation, “If your Amazon S3 bucket is configured for website hosting, you can configure a webpage redirect”. Even more specifically to my problem, it says that it can “redirect requests for an object to another object or URL”! Even more interestingly, there are powerful custom rules that can be used, so I was positive I could reach my goal of using a subdomain to redirect a specific URL. Let’s go step by step and see if it works as expected in the end.

Step by Step Guide

Step 1

First, pick your new FQDN. In my case it is “booktimewith.raphabot.com”. You now need to create a bucket with exactly this name and make sure it doesn’t block all public access.

Create bucket

Step 2

In your new bucket, go to the “Properties” tab and click in “Edit” for the “Static website hosting” panel.

Static Website Hosting

Step 3

Now, make sure to pick “Enable” and “Host a static website”. I know it seems counterintuitive when the other choice is “Redirect requests for an object”, but this option doesn’t allow redirects to URLs, only domains. For “Index document”, anything is fine, since we’ll never upload a file. Because it’s mandatory to have a value, I went with “index.html”. Now to the fun part, our redirection rule. Since we want it to always redirect, I tried to enter no Condition for redirection… and it worked! Then, the redirection rule is pretty straight forward. You can see a sample below:

[ 
    { 
        "Redirect": { 
            "HostName": "your.domain.com",  
            "HttpRedirectCode": "301", 
            "Protocol": "https", 
            "ReplaceKeyPrefixWith": "the/path/you/need/" 
        } 
    } 
] 

In my case, it looks like below:

Edit static website hosting

Step 4

Half the work is done now. Before moving on, go back to the properties tab, find the “Static website hosting” panel and copy the URL it generated for you. It should look something like http://bucket.name.s3-website-region.amazonaws.com:

Static website hosting URL

Step 5

Let’s move to Route 53. Here, I’ll assume you already have a domain registered and a Hosted Zone for it. So, pick your domain in your Hosted Zone list and click in the “Create record” button.

Create record

Step 6

For “Record name”, enter your subdomain and for “Record type”, make sure to pick CNAME. Now, for “Value”, it’s time to paste the valued you copied in the earlier step. However, we need to remove the protocol part. Click in “Create records”. Sample below:

Quick Create Record

All set

That’s it! Try to access your new record and see the results. If it doesn’t work right away, wait a few moments and try again. DNS records can take a while to propagate. It’s always DNS fault, not this guide’s. 😉

Let’s chat

If you want to book some time with me to discuss Skycrafters, cloud, career, or anything in between, make sure to head to http://booktimewith.raphabot.com and set some time in my calendar!

2 Likes

Question @raphabot, what would recommend doing in order to handle https natively so you are redirecting a https host to another https host?

1 Like