Hands on with Google Cloud Run

Sushil Kumar
4 min readApr 13, 2019

--

Its been couple of days since Google Next 2019 concluded. If you don’t know what Google Next is, it is an yearly organized cloud conference by Google to showcase their cloud prowess. Google also use this platform to launch and showcase new services in their Cloud Platform. This year was no different and Google launched a slew of products but the most highlighting product for me was Cloud Run. I will cover whats and hows of Cloud Run in this post. If you want to know what else was launched you can read this amazing article by Techcrunch or head over to Google Cloud’s YouTube channel to stream all of the sessions.

So lets get started, but before we jump into Cloud Run’s functionality let us take a quick detour to understand the rationale behind a service like Cloud Run.

Since the inception of public clouds, providers have been creating products and services to alleviate the pain of a developer in managing underlying infrastructure. Be it Heroku’s PaaS offering or Google’s App Engine where as a developer you just have to write you code and rest will be taken care off by the PaaS solution itself. These services really changed the landscape of how organizations deployed their solutions and gave them advantage to quickly iterate their product versions. With the use of these services you got built in scaling and high-availability. But it was not all sunshine, although the pricing was optimized but you still had to pay running cost even when you had zero traffic for hours, you still were limited in the choice of programming languages and framework you could work with.

In an attempt to deliver more cost optimized solutions cloud providers introduced serverless constructs like Lambda in AWS or Cloud Functions in GCP. You just had to write a stateless function and add a trigger of when to invoke it and let the platform take care of executing it. The cherry on top was that you just had to pay when your function service a request. This did solve the continuous pricing problem but you could write these functions in only a handful of languages. For example GCP supports only Python, Node and Go to write cloud functions. Also event triggered functions is not how most organizations develop their codes hence to leverage the platform they needed to re-write quite a bit of their codes which acted as an entry barrier.

Both PaaS solutions and Serverless functions were great way to host your web apps and web APIs without worrying much about your infrastructure.

Fast forward in 2019 Google has launched Cloud Run which makes all of your web apps go serverless without any extra effort. The only requirement that your web app has to fulfill is they have to be stateless and containerized.

In this post I’ll create a hello world web app with node, containerize it with docker and will deploy it in Cloud Run.

I’ll be using my Google Cloud shell to write my application. You can use your local machines to do so.

I’ll be using pretty standard Hello World node server example here.

var http = require(“http”)var port = process.env.PORT || 8080http.createServer(function(request, response){
response.writeHead(200, {‘Content-Tye’ : ‘text/plain’});
response.end(“Hello World”);
}).listen(port)
console.log(“Server is running at 0.0.0.0:” + port)

Save it as main.js.

Lets dockerize it.

FROM node:10.15-alpine
RUN mkdir $HOME/app
WORKDIR $HOME/app
COPY main.js .
CMD [“node”, “main.js”]

We’ll build the image using following command

docker build -t my-node-server:0.1 .

Next lets push this image to Google Container Registry so that we can deploy it using Cloud Run

docker tag my-node-server:0.1 gcr.io/[PROJECT-ID]/my-node-server:0.1docker push gcr.io/[PROJECT-ID]/my-node-server:0.1

With that done, let us deploy this node application on Cloud Run.

gcloud beta run deploy — image gcr.io/just-landing-231706/my-node-server:0.1

It will ask you to name your service. Press enter and let it choose image name as service name. Then it will ask you for region and whether or not to allow unauthenticated invocations to service. Choose the region of your choice and press ‘y’ for unauthenticated requests. Google will now deploy the service.

Output of deploy command

You can see the URL at which your newly deployed web app is available. Go to the URL to see if your app works.

Deployed Web App

And voila it works.

With Cloud Run, Google has really upped its game to provide a swift way to migrate on premise workloads to cloud without much re-writing.

In case you find any bug in my code or need any help in general please feel free to drop a comment below.

Till then Happy Coding :)

--

--

Sushil Kumar

A polyglot developer with a knack for Distributed systems, Cloud and automation.