Hosting a static website on Google Cloud with zero-scale costs
Sometimes, all you want is to host a static website. No fancy backends, no databases, just something serving plain HTML, CSS and JavaScript. I run a few websites and in my travels I’ve often wondered how I could use a Cloud Storage Bucket to host such a static web site. As it turns out Google has a helpful guide on how to set up a static website using buckets/
This method gets a little expensive when you tie it to a domain and you want to support both HTTP and HTTPS. For a domain, you need to pay Google to reserve a static IP address that you can refer to in your DNS records. You also need two load balancers, one to direct HTTP traffic to your bucket and one to redirect HTTPS requests to the HTTP load balancer.
Having tried this method I found there were some running costs associated with this approach:
I would estimate the costs at about £15/month, which feels like a lot money for a low-traffic web site. I’ve therefore created an alternative method for hosting a static web sites using Google’s own Cloud Run for which my costs are typically less than £0.05/month.
Cloud Run
Cloud Run is a really neat service. Let me try and explain what it does. If you are of the software persuasion you will have heard of Docker containers. These are essentially ways to package software so that you can run them anywhere. A container is a bit like a virtual machine in that it packages software with everything that it needs to run it, barring a kernel, but it’s a lot more lightweight than a virtual machine.
To use Cloud Run all you have to do is create a Docker container that contains a web server. You can upload this container to and Cloud Run will take care of the infrastructure for you. In this sense, it’s a bit like running a Kubernetes cluster that automatically scales with demand except that Cloud Run is more automated and supports zero-scaling. That is, if nobody is using your service you will incur zero costs. You do not have the overhead of a master node that needs to be running at all times, nor do you have to pay for load balancers.
Cloud Storage API
With Cloud Run it is possible to serve a static website from a Cloud Storage Bucket by creating a Docker container that acts as a web server and, upon any file requests, serves the files from your bucket using the Cloud Storage API.
The above design can be achieved with a very simple python script. It does have some limitations, though:
- There is no support for custom 404 pages.
- There is no support for directory listings.
- There may be resource constraints for large files.
Instructions
If you’ve decided to give this method a go, you can find detailed instructions on how to host your own static website in my GitHub repository. In particular, please read the README.md in that repository.
I am really interested to hear if anybody ends up using this method – let me know!