How to use GCP Cloud DNS and HTTPS redirects

Kevin P.
3 min readMay 8, 2020

--

Unlike Google Domains where you can easily create a HTTP(S) redirection in the console, there’s no basic way to do it on GCP. There has been a feature request opened for the last 2 years, but it is still in the same state.

Let’s see how we can achieve the same result in GCP.

GCP logo

Solution choice

The easiest way to do it in GCP is to use the Firebase solution. There’s a built-in function that allows to connect a custom domain, and to redirect it to another domain directly, including HTTPS support out-of-the-box.

This solution only works if you want to redirect to another domain (medium.com for example). If you need to redirect to a full URL like (https://medium.com/@printz.kevin/my_story), this solution won’t work.

They’re other ways to achieve the same result using GCP only, but they’re less easy (using a GCS bucket for HTTP redirection, and adding a load balancer if HTTPS redirection is needed)

Before starting…

Before jumping to the configuration, here are the requirements:

  • An existing GCP and Firebase accounts linked to a billing account
  • A domain name managed in Cloud DNS

Configuration

Firebase

Just go to the Firebase console, click on the “Hosting section”, and select “Add custom domain”.

Firebase console

Then, enter the domain you wish to redirect, check the box and add the destination url. It’ll work for both HTTP and HTTPS.

Create the redirection

If this is the first time you’re using this domain with Google, you’ll have to verify your ownership by adding a TXT record to the domain.

And finally, if everything goes well, you’ll have 2 A records to create to finalize the configuration.

Redirection summary

GCP

Now, you need to go to the GCP console, and select the Cloud DNS service. Open your zone, and click on “Add record set”. Finally, enter the DNS name, and the 2 IPv4 destinations.

Create Cloud DNS entries

Verification

The HTTP redirection should work directly, and redirect to HTTPS. The HTTPS one will take more time because it needs to create a certificate with Let’s Encrypt.

Redirect chain

After a few minutes (could take up to 1 hour), everything should be working!

Redirect from Safari
Redirect with curl

--

--

Kevin P.

SRE @ Somewhere_Redacted. | All opinions are mine.