Getting Started with Google Cloud Functions

Serverless architectures have been gaining wide traction among developers over the last couple of years. They're being promoted among developers as a way to focus on their code while letting the infrastructure vendor take care of running that code for them. It's not that serverless paradigms haven't been there in the past. Platforms as a Service (PaaS) have been around for years now and while they do achieve the goals to a large extent, the recent serverless platforms released by large vendors have been significantly different both in terms of features, what the developers need to do, and how they're priced.

Amazon Web Services (AWS) has been off the blocks for a couple of years now with their AWS Lambda offering. The last six months have seen the design of multiple reference architectures around AWS Lambda with close Integration with other AWS Services. Microsoft has Azure Functions, its serverless offering, and IBM recently introduced its OpenWhisk serverless solution under the Bluemix brand. This article is a technical tutorial on getting started with Google Cloud Functions, the serverless offering on the Google Cloud Platform (GCP). Google Cloud Functions finally entered beta at the recent Cloud Next 2017, Google's annual cloud conference.

What is Google Cloud Functions?

Google Cloud Functions, like other offerings, gives you the ability to define your computing in short snippets of code, which you can deploy and then run in response to events. As of its beta release, the only supported language for writing Cloud Functions is JavaScript.

High-level features of Google Cloud Functions are listed below:

  • Support for turning existing logic into callable functions that can optionally exist for only as long as the logic is active.
  • Cloud Functions are written in JavaScript and execute in a Node.js v6.9.1 environment on the GCP.
  • The billing occurs based on the number of invocations and the execution time, down to 100s of milliseconds. This provides immediate opportunities to organizations that seek alternative methods to autoscale while minimizing chances of overcapacity and any associated costs.
  • Integrated with other GCP Services like Cloud Datastore, Cloud Pub/Sub, and more.
  • Directly trigger Cloud Functions via a HTTP webhook. This is useful in triggering the Function from external services.
  • Use Cloud Functions directly from Firebase.

Getting Started with Cloud Functions

Let's get started. Now, you'll write your first function with Google Cloud Functions and execute it.

Google Cloud Functions executes the function based on events. Events can be triggered as a result of a database update in Cloud Datastore, a message being published in Cloud Pub/Sub, a BLOB getting uploaded to Google Cloud Storage, an event from Firebase, and more. These are called background functions, where they'll be executed in response to an event, which acts as a trigger. We'll take a look at various triggers that Google Cloud Functions supports a bit later in the article.

Google Cloud Functions also supports a foreground function (HTTP invocation). For example, you can directly invoke this function via a HTTP URL that's unique to your deployed Cloud function. Let's take a look at that.

Before you start, assume:

  • You have a GCP project
  • You have enabled Project Billing
  • From the API Manager, you have enabled the Cloud Functions API

To perform the above tasks, please refer to the official Documentation on Creating a Project, Enabling Billing and Enable/Disable APIs in your Google Cloud Platform project.

Cloud Functions is available from the main menu on the left of the screen as shown below. You'll find it in the Compute → Cloud Functions section.

Accessing Google Cloud Functions via the main menu, in the Compute > Cloud Functions section

Once you click on that, you'll be taken to the Cloud Functions functionality, and if you haven't deployed any Cloud Functions previously, you should see a screen that looks as follows:

Cloud Cloud Functions functionality overview

Click on Create function. This will take you to a form, where you can not only fill out the function details, but also use the inline editor to write your Cloud Function. Let's look at the form details in two parts, the first of which is shown below.

You'll provide a name for the function, use other defaults, and select the Trigger as HTTP Trigger option. Notice that the unique HTTP URL for your function is shown, too. Syntactically, it conforms to the following format:

Cloud Functions name and format

The next step is to write the Source Code for your function in an inline editor, so select Inline Editor for Source Code option in the Create function screen. This will show a helloworld function template in the inline editor, as given below:

Inline Editor for Source Code option in the Create function screen.

The inline editor is a convenient way to write and test out Cloud Functions but may not be the ideal way to write code in the long term. For that, you can use the source code editor of your choice (e.g., Atom, Sublime, etc.) and upload your source code as a ZIP file. You can also deploy it from the Cloud Source Repository, a private Git repository that is provided with each Google Cloud Platform project

Be sure to read the next Microservices article: Prerequisites to Consider Before Moving to Microservices