Google Cloud Run on Rails: a real life example (Part 1: preparing the ground)
Google Cloud recently launched Cloud Run, the new kid on the block from the serverless family and a game changer in many respects. In a more mature Cloud market, serverless services are becoming more and more attractive as they take care of many IT chores for you and abstract away any notion of server or infrastructure.
With this 4 part tutorial you’ll learn, step by step, how to deploy a non-trivial (read “not a Hello World!”) Rails application in production with Cloud Run. If you are not a Ruby on Rails developer, it is still worth reading this article as most of it still applies to other Frameworks as well.
I chose a simple yet substantial enough Photo Album application so as to exercise Cloud Run but also a number of other key GCP services typically involved in real applications. Namely:
- Cloud Build to build the container image of your application and deploy it to Cloud Run
- Container Registry to store and uniquely identify a version of your container image as well as check it against vulnerabilities
- Cloud SQL to manage your production MySQL database at scale
- Cloud Storage as the Rails Active Storage backend to store picture files
- Cloud Key Management Service (KMS) to protect your application secrets and credentials
Part 1 of this series introduces Cloud Run, guides you through the creation process of you GCP project and lets you choose your work environment.
Part 2 covers the Photo Album application and setting up the installation of Ruby and gems as well as running the Photo Album from your local environment (This part is optional and you may skip it altogether if you are not interested in running the Photo Album locally)
Part 3 is where we prepare our production environment on GCP, setting up Cloud Run, Google Cloud Storage to manage image files, Cloud SQL as our relational database and, most importantly, secure all the secrets and credentials before deploying in production.
Part 4 shows how to run Cloud Build to build the application container and publish it on Cloud Registry so that Cloud Run can deploy it. We also address the application monitoring.
Making it to the end of this tutorial will provide you with a solid and fully working foundation that you can use as a basis for any professional Rails application.
Cloud Run: the best of both worlds
A number of serverless services have been available for quite some time from several Cloud providers. More often than not the benefits of abstracting away infrastructure management comes with a number of restrictions such as a predefined list of frameworks to choose from or a limited support for custom libraries.
Wouldn’t it be great to take advantage of all the serverless benefits while retaining the ability to run your own software stack and application? This is exactly what Cloud Run does.
Cloud Run turns your custom-built container image into a fully managed application. It comes with a number of appealing features:
- It provides auto-scaling including scaling down to zero which means that when your application is not working (e.g. no HTTP requests coming in) you are not charged.
- HTTPS support comes out of the box as well as custom domain name
- The granularity of billing is down to 0.1 second and Cloud Run comes with a comfortable (and recurring!) monthly free tier.
- Last but not least, Cloud Run is Knative compliant which means that you can deploy your container along with its services file to either fully managed Cloud Run, or on GKE clusters with Cloud Run on GKE or on any other Kubernetes cluster of your choice.
The only thing that Cloud Run is asking from your application is that a) it comes in the form of a container image and b) it exposes an HTTP/S server on a port number of your choice.
Creating your GCP account and project
At this point we assume you already have a GCP account. If not, create a free trial account now.
Once you have logged in the GCP Web Console, create a new project by click on the project pull-down menu in the top menu bar and click ‘NEW PROJECT’. Use ‘Photo Album’ as your project name and click ‘CREATE’ at the bottom of the dialog box.
I strongly advise creating a new dedicated project instead of using an existing one. By doing so, in case you don’t plan to keep the resources used in this tutorial, you can simply delete the project at the end, removing all resources associated with it at once.
Wait for a few moments until the project creation completes and make sure to select the Photo Album project you have just created from the project select box at the top of the screen. You should now see something similar to the screenshot below.
Also check that billing is enabled for your GCP project. Fear not, your free trial account comes with a $300 credit that will amply cover the cost of running this tutorial so you won’t be charged a dime. On top of that several of the services we are using come with comfortable one shot or monthly free tiers.
Take note of the Project ID on the project dashboard. You’ll need it in the next section.
Setting up your work environment
Most of the instructions in this tutorial can be executed either from the Google Console Web UI (as we just did to create our new project) or from the command line. In this article we will be using mostly command lines and, most notably the gcloud command from the Google Cloud SDK.
There are 2 ways to setup your work environment: either by using a GCP Cloud Shell session or by installing the Google Cloud SDK on your local machine. Either one is fine for this tutorial. If you want to save time setting up your environment on your own machine then go for the Cloud Shell option.
Using Cloud Shell
The easy way to setup your work environment is to use a GCP Cloud Shell session by clicking on the Activate Cloud Shell button at the top of the GCP Web console window.
The GCP Cloud Shell comes for free with each GCP user account. It basically provides everything you need for this tutorial. It already has your project and login credentials configured, the Google Cloud SDK is pre-installed, a shell, a code editor and a Web previewer are also available. All of that in one place.
Setup the project ID environment variable in your shell session (if you ever have to reconnect to your shell session remember to set it up again):
Using your own work environment
Alternatively, you may decide to work from your own machine. If so you have to install the Google Cloud SDK before going any further.
At the time of writing, Cloud Run and some of its options are still in beta so we need to update the freshly installed Cloud SDK with beta components.
$ gcloud components install beta
$ gcloud components update
Note: if the first command displays an error message saying that beta components cannot be installed this way then follow the instructions shown.
Then authenticate yourself with GCP and set the project you’ll be working with:
$ gcloud auth login your_account_name
$ gcloud config set project $PROJECT_ID
where your_account_name is the email address you used to create your account.
Activating GCP APIs
As stated earlier, in the course of this tutorial we will be using a number of GCP Services. To interact with those services we need to enable the corresponding Google Cloud API.
Notice that we are in a position to enable those APIs because as the creator of the project you are automatically granted the ‘Project Owner’ role which basically means that you are almighty on your project.
$ gcloud services enable run.googleapis.com # Cloud Run API
$ gcloud services enable sqladmin.googleapis.com # Cloud SQL API
$ gcloud services enable cloudkms.googleapis.com # Cloud KMS API
Congratulations! You have just completed Part 1. Your GCP Photo Album project is created, you have decided on your preferred work environment and relevant APIs have been activated. In Part 2 we’ll run the Photo Album application locally in your work environment.