Building Serverless on Google Cloud with Cloud Run

Cloud Run has come a long way since it was first introduced as an extension to Google Cloud Anthos (Google’s former app modernization platform). 

It’s now a standalone platform, capable of handling different types of workloads serverless, including web services, background jobs, and more. But because Cloud Run has several components for handling these workloads, it can be challenging to navigate.

Choosing the ideal solution for your workload and getting the best out of this platform is no easy feat. So, in this article, we’ll break it down, looking at Cloud Run in detail. By the end of it, you’ll know what exactly Cloud Run is and which workloads are a great fit for it.

What is Cloud Run?

Cloud Run is a fully managed, serverless compute platform that allows you to run containers directly on Google Cloud’s scalable infrastructure. Cloud Run is simple and automated, meaning you don’t have to create clusters or manage infrastructure. Google does it for you. All you have to do is bring your code, and Cloud Run will handle the rest. 

The purpose of Cloud Run is to make it easy for developers to deploy any containerized workload easily on Google Cloud’s robust infrastructure. It abstracts you from most of the things you would have to do to execute containers. This way, you can focus on code and be more productive.

What are Cloud Run’s key features?

As Google Cloud’s serverless container execution platform, Cloud Run has the following key features:

It is language-agnostic

Cloud Run enables you to deploy code written in any language, provided you can put it in a container. However, creating a container image manually is completely optional. To be specific, if your code is written in Node.js, Python, Go, or Ruby, Cloud Run has an option that builds the container for you.

Fast autoscaling

As incoming requests to your service increase, Cloud Run automatically adds instances to meet these new demands. It will similarly reduce instances when traffic goes down. It can even scale down to zero, meaning no instances will be up if your app isn’t being used. This makes Cloud Run cost-effective because you’ll only pay when the app is being used.

GPUs for AI workloads (in Preview)

Recently, Google Cloud announced that Cloud Run will now support NVIDIA L4 GPUs. This opens up Cloud Run to more resource-demanding use cases, such as:

  • Building custom chatbots with lightweight models, such as Google’s open Gemma or Meta’s Llama 3. 

  • Serving gen AI models finetuned with your enterprise data.

  • Compute-intensive services such as 3D rendering.

NVIDIA’s GPUs give Cloud Run the capacity to handle AI inference workloads, especially for applications that demand real-time processing.

When Cloud Run launched, it only supported stateless containerized workloads that could be invoked with HTTP requests. This was the only way to run code on it. Now that Cloud Run is a platform, it supports more types of workloads.

Ways to run your code on Cloud Run

Cloud Run provides you with three ways to run code on it. They include:

  • Cloud Run services.

  • Cloud Run jobs.

  • Cloud Run functions.

Each of these methods is suited to a different type of workload.

1. Cloud Run services

This is what most people think of when they talk about Cloud Run, because it was the only way to run code on this platform initially. A Cloud Run service is used to run code that responds to web requests or events. 

In services, Cloud Run provides the infrastructure needed to run a reliable HTTPS endpoint. What you’ll need to do is ensure your code listens on a TCP port and handles HTTP requests.

With that in mind, what workloads are a great fit for Cloud Run services? Here are some examples:

  • Websites and web applications.

  • Microservices and APIs - This includes REST, GraphQL, and gRPC APIs.

  • Streaming data processing with Pub/Sub.

Cloud Run services come with all of Cloud Run’s built-in features, such as support for NVIDIA GPUs, autoscaling, etc. When it comes to autoscaling, every Cloud Run service has an internal load balancer. This load balancer is responsible for adding and removing instances to meet demand.

Manually scaling Cloud Run services (in Preview)

By default, Cloud Run services automatically scale your instances depending on demand. Now, it supports manual scaling on top of this. This will be useful when you want to set a specific number of instances or prevent Cloud Run from scaling to zero.

2. Cloud Run jobs

Some workloads are not suited to run as Cloud Run services. For instance, those that don’t listen for HTTP requests and their containers run to completion.

For these types of workloads, Cloud Run has the Cloud Run jobs extension. Cloud Run jobs is suitable for code that performs a task, then stops, let’s say a script to run a database migration. 

It is also suitable for:

  • Background processing – for example, if you have an e-commerce store and customers have abandoned their carts, you can use Cloud Run Jobs to send email campaigns for these abandoned carts.

  • Batch data processing – especially the extraction phase of ETL.

How it works

When you run a job, Cloud Run will automatically spin up the instances needed to execute it to completion. Once done, Cloud Run’s autoscaling feature will scale it to zero until the next job starts. You don’t have to manually take instances down once the job is complete.

Since these jobs don’t serve HTTP requests, how can you get them to run? Google Cloud provides several ways to do this depending on your needs. To execute a Cloud Run job directly, you can use:

  • Google Cloud Console - Here, you select the job you want to run on your Cloud Run jobs page and click ‘Execute’.

  • Google Cloud CLI - Using the gcloud run jobs execute to trigger the job.

You can also schedule a job using Cloud Scheduler. This is ideal for periodic tasks such as generating reports or backups.

Finally, you can integrate your Cloud Run Job with other Google Cloud Services such as Workflows, Pub/Sub, and Cloud Storage. This way, specific activities, for instance, when Pub/Sub publishes a message on a given topic, trigger the Cloud Run job.

3. Cloud Run functions

What if you don’t want to deploy an entire service or job on Cloud Run, but a code snippet? For this, you can use Cloud Run functions. Cloud Run functions provide a simple way to run small, single-purpose functions directly on Cloud Run. 

You deploy functions as services on Cloud Run and configure them to react to HTTP(S) requests or events from Pub/Sub, Eventarc, etc. These functions must be written in a supported language such as Node.js, Python, Go, etc.  

This is similar to running a Cloud Run service, except here, you don’t have to create a container image. You only provide the source code. The function will run as a service, scaling up and down automatically based on events.

Cloud Run functions are suitable for several use cases, such as:

  • Deploying lightweight APIs and webhooks.

  • Consuming raw data from Pub/Sub and enriching it with more information.

  • Analyzing audit logs for suspicious activity.

Long story short, Cloud Run functions are ideal for writing isolated business logic.

Update: Cloud Functions(2nd gen) is now Cloud Run functions

Google has merged the Cloud Functions infrastructure with Cloud Run to form Cloud Run functions. Now, Cloud Run functions users have access to Cloud Run’s newest features, including the NVIDIA GPUs for AI workloads. 

As a result of this unification, functions are now a deployment option in Cloud Run. You can write functions and deploy them to the Cloud Run infrastructure. Although so much has changed, Cloud Run functions retains most of its previous features, such as support for existing APIs and gcloud commands. 

Cloud Run’s integrations on Google Cloud

Cloud Run is tightly integrated with Google Cloud’s broad ecosystem. Let’s look at some of the most important integrations you’ll want to use together with Cloud Run.

Data storage

It’s hard to think of a web app that doesn’t need to store data. This is why Cloud Run integrates with so many of the data solutions on Google Cloud, including:

  • Cloud SQL - A fully managed relational database service for MySQL and PostgreSQL.

  • AlloyDB - For demanding PostgreSQL workloads.

  • Cloud Storage - For storing unstructured data.

  • And more.

Cloud Tasks

You can offload your app’s tasks outside of a user request using Cloud Tasks. Let’s say a user places an order in your app. Instead of processing the payment and sending a confirmation email synchronously, Cloud Tasks allows you to do this in the background. This prevents your app from slowing down.

Secret Manager

With Secret Manager, you can securely store your database passwords, API keys, and certificates. This helps to prevent your sensitive data from falling into the wrong hands.

Cloud Run’s pricing

Cloud Run’s pricing is based on the resources your containers consume. You’re billed only for the time your instances are actively processing requests(for services) or executing jobs.

You’ll also be charged per million requests, CPU and memory allocation, and for data transferred outside your instance. What you can take away from this is that Cloud Run has a dynamic pricing model, which you’ll understand after you specify the resources you use. For more direction, you can check Cloud Run’s pricing page.

Summary

Cloud Run makes it easy for developers to run containers directly on Google Cloud’s scalable infrastructure. It is a serverless solution, meaning it handles infrastructure provisioning and management for developers, allowing them to focus on code.

Although it started as an extension for Anthos, Google has worked to evolve it into a standalone platform packed with features to handle different types of workloads. To top it up, its tight integration with Google Cloud’s ecosystem means more features and a better deployment experience for developers.


Next
Next

Google Cloud Next'25 Recap