This post is now deprecated

This post might have outdated content and is no longer maintained.

A Comprehensive Guide to Google Cloud Functions

Introduction

Cloud Functions allow us to write business logic without provisioning any infrastructure. In this post, we will talk about the flavors of cloud functions available at our disposal, limitations, development/deployment workflow, local development with cloud functions emulator and code.

Google describes Cloud Functions as following: —

Google Cloud Functions is a lightweight, event-based, asynchronous compute solution that allows you to create small, single-purpose functions that respond to cloud events without the need to manage a server or a runtime environment

Without further ado, let’s dive in.

Cloud Functions Runtime and Dependencies

Cloud Function are modules written in Javascript and run in a Node runtime. The Cloud Functions execution environment follows the Node “LTS” releases, the current Node version running in Cloud Functions is Node v6.11.1.

For dependencies, there is only good news as you can use the powers of npm and package.json

P.S. when deploying from local machine as we will see further in this post, you can deploy configuration files/credential keys for access (not advised to version control the credential keys though). Use Google Cloud Deployment Manager for provisioning configuration.

Types of Cloud Functions

Google Cloud Functions come in two flavors: —

Foreground (HTTP) Functions

https://us-central1-divu-178107.cloudfunctions.net/hello-world

Writing an HTTP Cloud Function is pretty straight forward, you export a function with the request, response arguments (like express). This is how the code for the above function looks like:

https://gist.githubusercontent.com/divyenduz/207a2a7d2f0397c372d76acf25a75989/raw/c6973b3db9204b822c5d00da8d58fda44a9f57bc/divu-hello-world-gcf.js

Background Functions

This flavor of Cloud Functions allows us to handle events from our cloud infrastructure, such as listening to messages on a Pub/Sub topic or changes in a Google Cloud Storage bucket. Suppose you want to write a cloud function that resizes images to different variations on upload to Google Cloud Storage. That function would look something like this.

https://gist.githubusercontent.com/divyenduz/96e4d633c9b24df9bbcaf0009303e07f/raw/21fa69c6439d74d7080d41ace2da1079d714bbf3/resize-gcf.js

Important note from the docs.

Note: You should always either invoke the callback argument, or return a Promise when your function has completed. Otherwise, your function may continue to run and be forcibly terminated by the system.

Also, refer to the docs for details on event parameters and more information on the callback.

How to create Foreground (HTTP), Background Function

You get to choose the type of function you want to create while creating a Cloud Function via choosing a trigger and this is how you choose between creating different types of functions.

Options to trigger a google cloud function
Options to trigger a google cloud function

The same can be done from command line as well using gcloud utility as follows:

gcloud beta functions deploy <function-name> --stage-bucket <bucket-to-store-the-function-to> --trigger-http # Deploy HTTP functiongcloud beta functions deploy <function-name> --stage-bucket <bucket-to-store-the-function-to> --trigger-bucket=<bucket-on-which-we-want-to-listen-events> # Deploy background function

You can also call functions from command line to test them as follows:

functions call <function-name> --data='{"message":"Hello World"}' # HTTP functionfunctions call <function-name> --data='{"name":"<file-name-that-fake-triggered-this-event>","bucket":"<bucket-name-that-fake-triggered-this-event>"}' # Background function

Lastly, cloud functions have quotas that we should be aware of.

Meta information about cloud functions

I added this as a bonus in this section because I feel this is important. You have access to /tmp to store temporary files according to the docs.

Local DiskCloud Functions provides access to a local disk mount point (/tmp) which is known as a "tmpfs" volume in which data written to the volume is stored in memory. There is no specific fee associated with this however writing data to the /tmp mountpoint will consume memory resources provisioned for the function.

This is really handy if you want to process files and need a temporary storage before putting it in Google Cloud Storage bucket.

Also, to upload files, you need to convert them to base64 encoded strings (I could not find ability to upload files otherwise in docs, please correct me if I am wrong). Cloud Functions use body-parser and hence the limitation.

Description of parsing done by HTTP functions
Description of parsing done by HTTP functions

Ending the meta information section with this nice blog post, that talks about sharing functions between Google Cloud and Express.

Development and Deployment Workflow

While it is possible to write cloud functions from the google developer console, deploy it and test it from there, it would make for a terrible developer experience as a cloud function may take up to 2 minutes to deploy.

Enter Cloud Functions Emulator, whilst still in alpha at the time of this writing, it is a great tool that helped me a ton while working with Cloud Functions. You can install the emulator using the following command.

yarn global add @google-cloud/functions-emulator

Once setup, the emulator can be used with functions command. Here is a list of commands (not exhaustive) available at your disposal.

functions --help
functions <command> --help
functions start
functions stop
functions kill
functions deploy helloWorld --trigger-http
functions call helloWorld --data='{"message":"Hello World"}'
functions list
functions status
functions debug <function-name>
functions inspect <function-name>

Notice how closely the functions emulator follows the syntax of gcloud command to deploy and call functions. This caters for an amazing development experience as now you can write, test, automate-tests for your functions locally and version control them before actual deployment.

You can also deploy Cloud Function directly from source control.

Next steps

Play around with Google Cloud Function for fun and glory.