You are currently viewing Google Cloud Run – Tutorial

Google Cloud Run – Tutorial

There are occasions when one or more serverless functions are not sufficient to represent a service. For these cases, there is Google Cloud Run on the Google Cloud Platform. Cloud Run provides two ways to run containers. Services and Jobs. In this example, a Google Cloud Run service is defined using Terraform, which is called regularly based on a scheduler job. Cloud Build is used to publish the current code to the service.

tl;dr

Using Terraform, all necessary components can be created to continuously update a cloud run service from a Github repository. A highly simplified Flask web service is used as an example. Using Cloud Scheduler, this service is called regularly.

Requirements

Before this kind of service can be set up, some requirements must be met. A Google Cloud Project is required, as well as a Github account. On the computer that will be used for development, Terraform, Google Cloud SDK, git, Docker and Python must be installed. In this example Python is used, but it is possible with any language that can be used to create a WebServer.

  • A Google Cloud project can be created at Google Cloud Platform. Only a Google Account is needed, new customers get a free credit of 300€ for 90 days.
  • If not already available, a free Github account should be created on Github. The example can also be implemented using Google Cloud Source Repositories. As an alternative to Cloud Build, Github Actions can be used.
  • Terraform, Google Cloud SDK, Docker and Python must be installed on the computer used, for which a package manager such as Homebrew or Chocolatey is recommended. Linux users should use the one included in their distribution.
  • If you do not want to install anything, you can also use the Google Cloud Shell, which can be found in the Cloud Console.

APIs that need to be enabled in Google Cloud

In addition to the software requirements, some APIs need to be enabled on the Google Cloud Platform:

  • Cloud Run API
  • Cloud Build API
  • Artifact Registry API
  • Cloud Scheduler API
  • Cloud Logging API
  • Identity and Access Management API

The Cloud Run API is needed to build a Cloud Run service, the Artifact Registry is needed to store the container images. The Cloud Build API and the Identity and Access Management API are needed to implement a CI/CD pipeline. Cloud Scheduler is used in this example to invoke the service on a regular basis. Since all services are created and managed via Terraform, the APIs are required.

Infrastructure

The creation of the infrastructure runs in several steps, only if app code and container already exist, the whole process can be automated. For the definition of the infrastructure a folder Infrastructure is created and in it the files main.tf, variables.tf and terraform.tfvars.

For the full code of this example see: https://data-science-blog.com/blog/2022/11/08/google-cloud-run-tutorial/

Summary

Using Terraform, it is possible to create a fully code defined, continuously published Google Cloud Run service. This is done by using GCP services, wrapping a Flask webapp in a container and publishing it on Cloud Run.

DATANOMIQ is the independent consulting and service partner for business intelligence, process mining and data science. We are opening up the diverse possibilities offered by big data and artificial intelligence in all areas of the value chain. We rely on the best minds and the most comprehensive method and technology portfolio for the use of data for business optimization.