Now we have the website deployed on the Google Cloud Platform as a static site, it allows us to access it by going to http://www.winsnes.io. This is fantastic, but what if someone goes to http://winsnes.io?

This is the second post in the ongoing series about hosting a blog on the Google Cloud Platform for cheap.

When we set up a storage bucket with a static site hosted on it, we ask Google to take care of all the domain name routing that happens in the background. And Google will happily route all requests to www.winsnes.io to the static buck and display the content of the blog. But we never told google to do anything about winsnes.io, so that won’t be routed correctly.

In the previous post we talked about how you can ford a sub domain to a different url using a cname, but not a root domain. More details on why that is can be found in this blog post by Dominic Fraser. What that means is that we can’t create a cname for winsnes.io, we will have to use an a record. Some name server services, like AWS R53, support something called an aname or an alias which works as a cname but for root domains. Google sadly doesn’t support this yet, so we have to find a different way to manage that.

To create an a record you need to have a static IP that you can point it to. In the Google Cloud Platform, you can get this a few different ways, like a virtual machine, but we are going to use the Google Load Balancing service, as it gives us an easy and scalable way to redirect traffic that can be managed through Terraform.

Deploying a Load Balancer using Terraform

To deploy a load balancer in the Google Cloud Cloud platform is a little different than other clouds. You deploy four different resources that together allows you to route traffic to where you need it to go. It is a bit odd if you’re used to traditional load balancers, but it is very flexible.

First you create a Global Forward Rule which is the public endpoint of the load balancer. This is then connected to a Target Http Proxy which links the forward rule to the URL Map. The URL Map contains all the routing rules allowing you to route different requests to different backends. E.g. route all requests to winsnes.io/static/ to a storage bucket, while all requests to winsnes.io/api goes to a VM to handle the dynamic request.

In our case we are routing all requests to the same place, so we have a single rule in place. This rule routes all requests to the backend bucket service containing our blog.

The Terraform added to the main.tf file to accomplish this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
resource "google_compute_global_forwarding_rule" "default" {
  name       = "winsnesio-global-forwarding-rule"
  target     = "${google_compute_target_http_proxy.default.self_link}"
  port_range = "80"
  depends_on = ["google_compute_target_http_proxy.default"]
}

resource "google_compute_target_http_proxy" "default" {
  name        = "winsnesio-http-proxy"
  description = "Http proxy for winsnes.io"
  url_map     = "${google_compute_url_map.default.self_link}"
}

resource "google_compute_url_map" "default" {
  name            = "winsnesio-url-map"
  description     = "Url map for winsnes.io blog"
  default_service = "${google_compute_backend_bucket.static_storage.self_link}"

  host_rule {
    hosts        = ["${var.root_domain}"]
    path_matcher = "allpaths"
  }

  path_matcher {
    name            = "allpaths"
    default_service = "${google_compute_backend_bucket.static_storage.self_link}"

    path_rule {
      paths   = ["/*"]
      service = "${google_compute_backend_bucket.static_storage.self_link}"
    }
  }
}

resource "google_compute_backend_bucket" "static_storage" {
  name = "storage-backend-bucket"
  bucket_name = "${google_storage_bucket.static-store.name}"
}

After deploying this new load balancer, we will have a static IP that we can now use to update our DNS entry for winsnes.io. After the DNS changes have replicated to the major DNS servers, usually a couple of hours depending on the TTL of the entry, we should now be able to access the blog on http://winsnes.io!