Using Google’s Cloud Platform to create a Serverless application backend

Clouds in a blue and pink sky. Photo by Dan Aragón on Unsplash

Serverless. Big buzzword in 2017. According to Stack Overflow 2018 survey it’s the second most loved platform for developers. Companies like Amazon, Microsoft, Google amongst others are investing heavy in this technology and it seems to only get bigger and bigger. In case you don’t what it is and why it’s getting so much attention, I recommend the free ebook What is Serveless from O’Reilly. If you’re interested in start writing your first application on it, you came to the right post.

In one of the course that I’m taking in college, we have been asked to create an application that uses as many of cloud products/services as possible from one of the big vendors in the market: Amazon’s AWS, Microsoft Azure or Google Cloud Platform (also known as GCP). My team decided to use the latter to redo one of our previous assignments, a sliding puzzle and I was in charge of coding the backend of the application. In this article I’ll show you how I combined several GCP’s products to create my serverless function.

Before we start…

First, here’s what you’ll need to follow this tutorial:

  • A GCP account: If you have a Gmail account and a credit card, go to GCP website and sign up for a $300 credit to spend over a 12 months period.

Additionally, for every GCP product that we use, you’ll need to do 3 things:

  1. Select or create a Cloud Platform project.

Note that some products on GCP are in beta, here’s a excerpt from its website:

This is a beta release of Google Cloud Functions. This API might be changed in backward-incompatible ways and is not subject to any SLA or deprecation policy.

So if you’re reading this tutorial in the distant future, not everything might work as described here.

Also important, as the writing of this post, there are 2 differents products that runs functions on GCP: Cloud Functions and Firebase Functions. As we’ll push data to firebase, go with the last one.

Let’s get started!

Here’s a step-by-step on how our backend works:

  1. An image for the sliding game is uploaded to a bucket on Cloud Storage. This event will trigger a Cloud Function

A flowchart of the process:

There are 4 different products are involved in this process: Cloud Storage, Cloud Functions, Cloud Vision, Firebase and Logging (it seems that Google is not too inventive when it comes to names their products).

Installing Firebase Functions

To start writing your functions, install firebase globally using the command:

npm install -g firebase-tools

After that, go to the folder where you want to create your function and login and authenticate your account executing:

firebase login

Finally, run the command below to install other dependencies and create your first function:

firebase init functions

Create and deploy your function

After running the last command, you’ll notice that a folder structure was created. Inside folder firebase-function>functions you’ll find a file index.js, that’s the starting point of your functions now.

On index.js, enter the following code:

const functions = require("firebase-functions");
exports.processImage = functions.storage.object().onChange(event => {
const object = event.data;
console.log("Processing file: " + event.data.name);
return;
}

Done! This function will be fired every time a file is uploaded to the bucket associated to the same project of this function. To deploy the function, run:

firebase deploy — only functions

How to test your function

To test your function, all you need to do is upload a file to your bucket associated with the same project of the function. As the function simple spits out the name of the file you uploaded, go to Logging and you should see something like this:

It’s possible to test functions locally, but you’ll need to set up credentials for the APIs that you require inside your code, and that’s beyond the scope of this tutorial.

Uploading a file everytime we want to fire the function is a big annoyance, but luckily there’s another way. Head over Cloud functions console and you’ll see your deployed function:

Clicking on the function name will show a screen with more details of it. Go to the Testing tab, and you’ll notice a big Triggering event input box.