Cloud Run Using Pubsub Triggers

Jonathan Campos
Aug 26 · 5 min read

A few months back Google announced something really cool, a little known project called Cloud Run — and from the initial announcement, Cloud Run caused some serious noise. Cloud Run comes out at a very interesting time. Just as developers are coming in mass to Kubernetes, Google came out with a new tool that makes it so whether you know the difference between a Deployment and an Ingress you can still create amazing tools that run built on the power of Kubernetes and Google’s GKE.

If you want to go further down the rabbit hole you’ll learn that under the covers Google is using another new project that is gaining steam called KNative. Knative uses the power of Kubernetes and — with some black magic — turns Kubernetes into a Serverless platform that allows you to ramp up your workload based on usage and ramp down your workload all the way to zero if there is no traffic.

With Cloud Run the big benefit is with just your Dockerized application and a few clicks you can have your application running on Kubernetes in an environment you can trust to scale up and down as your usage scales.

Now, in this article, I am going to quickly show you that you can have everything running in no time. Without further ado, let’s get started.

Cloud Run Example Project Using Pubsub and Scheduler

As a quick little project, we are going to make a system that uses Pubsub to trigger your Cloud Run workload. I felt the easiest way to trigger these Pubsub messages was to use Cloud Scheduler to trigger Pubsub on an interval that we can set (every minute in this example). What will this look like? Like this.

Cloud Scheduler To Cloud Run via Pubsub

Our Cloud Run workload is written in NodeJS but if you look at the final code you’ll see that the language doesn’t really matter for this example because of how simple it is.

All of the code is viewable in my Github Repo.

Setting Up A Basic Node Project

The NodeJS project is fairly simple. A tiny Express application and one POST endpoint for our PubSub subscription to push data into.

Index.js File Consuming Pubsub Message

You can look deeper into each line of code in the getting started project in Github.

Build And Deploy The Project

To build the project into a deployable Dockerized application we just need to run one quick command to build and store the application within GCP.

Dockerize Application

This is it! This is the moment. The all-important moment where we publish our application to Cloud Run. Are you ready for this?

Deploy To Cloud Run

That’s it! That is how hard it is. Okay, it is crazy easy and now completely scalable. Seriously, this is basically production-ready.

Cloud Run Project Running

Everything from now on is downhill to support our application.

Add Pubsub

Our application is running but nothing is really happening at this point because we don’t have Pubsub wired up. We will now add Pubsub into our GCP Project.

Enable Pubsub

This script enables Pubsub and creates a Pubsub Topic.

Created Pubsub Message

The stage is set. We just need to set up the Cloud Scheduler dispatch the Pubsub Topic.

Add The Scheduler

Cloud Scheduler is a really slick tool that allows you to set up a Cron schedule that will fire off either an HTTP message or Pubsub message on a regular interval. For our project, we are having Cloud Scheduler send out a Pubsub message every minute to simulate an application dispatching Pubsub messages. Again, one line of bash script later and we are good to go.

Create a Scheduler Job

Cloud Scheduler is now dispatching the topic every minute.

Created Cloud Scheduler

The last thing we need to do is set up a subscription to push the topic into our Cloud Run workload.

Add A Subscription

This is the last/most complicated part of the process. We need to create a service account, get the URL for our Cloud Run endpoint and then create the subscription to route the Pubsub messages to Cloud Run.

First, the URL. This can be found in the Cloud Run dashboard very easily.

Cloud Run URL

Next, we need to create the subscription. This is done with a few lines of a bash script.

Create Subscription

As soon as this is completed the Pubsub messages will be routed to Cloud Run. Let’s go see our handy work.

Watch The Logs Roll In

When we return to Cloud Run we can quickly see that something is already running.

Results coming through Cloud Run Metrics

And if we go to the logs for this Service we can see the data as it comes through.

Logs Showing Results

And we are officially done. Everything is running through into our service using Pubsub via Cloud Scheduler. It’s pretty slick to see if all running.


Cloud Run is an amazing service. If you have a Google Kubernetes Engine (GKE) instance running you can include Cloud Run as an Add-On or you can use the managed Cloud Run instance as we’ve used here. Either way, you will have an amazing platform to build the biggest applications.

Jonathan Campos is an avid developer and fan of learning new things. I believe that we should always keep learning and growing and failing. I am always a supporter of the development community and always willing to help. So if you have questions or comments on this story please add them below. Connect with me on LinkedIn or Twitter and mention this story.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Jonathan Campos

Written by

Excited developer and lover of pizza. CTO at Alto. Google Developer Expert.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade