Google Cloud Functions Tutorial : What is Google Cloud Functions?

This is part of a Google Cloud Functions Tutorial Series. Check out the series for all the articles

In the first part of the series, we took a high level look at the Computing options that are available today and specifically at Functions as a Service (FaaS) features that we should expect from anyone providing that offering.

In this post, we are going to take a look at what is Google Cloud Functions? As per the official documentation:

Google Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your Cloud Function is triggered when an event being watched is fired. Your code executes in a fully managed environment. There is no need to provision any infrastructure or worry about managing any servers.

Focus on the words that I have highlighted in bold:

  • First up is serverless and this is term that you should be familiar now. This word is directly linked with other words in bold i.e. code executes in a fully managed environment and no need to provision any infrastructure. This is exactly what we would expect or have come to expect from a Serverless offering.
  • single-purpose functions : The unit of work that is expected from the developer is what we know as functions in any programming language. All you need to write are short snippets of code in any language that is current supported by the FaaS provider.
  • attached to events emitted from your cloud infrastructure and services and triggered when an event being watched is fired: How is your function going to be triggered or executed? There is always a way to directly invoke your function but the interesting cases come in when they are invoked indirectly or in asynchronous fashion. So for e.g. what if you had to send an email when a file is uploaded to Google Cloud Storage. In this event, the event is a file upload to Google Cloud Storage. In summary, you define and configure your function that it will be triggered on file upload to Google Cloud Storage. Now, when a user uploads a file to that bucket, the event is fired and since your function registered to be triggered when that event occurs, Google Cloud Functions will invoke your function and pass the details of the event (file uploaded, id, time, date, etc) as a parameter to your function. This way you get information on that event and can do your processing.

Google Cloud Functions — Execution Environment

Here are 3 key things to keep in mind while dealing with Google Cloud Functions. Again this applies at the time of writing and is likely to change as it gets out of BETA and announces support for more languages, event providers and so on.

  • Cloud Functions are in GA (General Availability).
  • Cloud Functions supports multiple runtime environments (some in GA, Beta and Alpha). They are:
    - Node.js v6.14.0 (GA)
    - Node.js v8.11.1 (Beta)
    - Python 3.7.0 (Beta)
    - Go (Alpha). Apply for early access.
  • Based on the above runtimes, it should be clear that you can currently write Cloud Functions are in JavaScript, Python or Go.

How does Google Cloud Functions work?

For this, I will take the liberty of using a diagram from the official documentation:

Focus first on the left side labeled as Cloud Services. This is the Google Cloud Platform and its various services. Think of all the services like Google Cloud Storage, Google Cloud Pub/Sub, Stackdriver, Cloud Datastore, etc. They all have events that happen inside of them. For e.g. if a bucket has got a new object uploaded into it, deleted from it or metadata has been updated. Similarly if a new statement has been added to Stackdriver logging or Cloud Pub/Sub getting a message published at a particular topic and so on. Not all events might be supported right now or in other words, not all Cloud Services or Event Providers might be supported right now in Google Cloud Functions but some of them do.

Now, when an event for e.g. Object uploaded into a Bucket in Cloud Storage happens, then an event is generated or fired or emitted. The Event data associated with that event has information on that event. If your Cloud Function is configured to be triggered by that event, then the Cloud Function is invoked or run or executed. As part of its execution, the event data is passed to it, so that it can decipher what has caused the event i.e. the event source, get meta information on the event and so on and do its processing. As part of the processing, it might also (maybe) invoke other APIs. These could be Google APIs or external APIs too. It could even write back to the Cloud Services.

When it has finished executing its logic, the Cloud Function mentions or specifies that it is done. Multiple Event occurrences will result in multiple invocations of your Cloud Functions. This is all handled for you by the Cloud Functions infrastructure. You focus on your logic inside the function and be a good citizen by keeping your function single purpose, use minimal execution time and indicate early enough that you are done and don’t end up in a timeout. This should also indicate to you that this model works best in a stateless fashion and hence you cannot depend on any state that was created as part of an earlier invocation of your function. You could maintain state outside of this framework via some other service like Shared Memcache and so on.

Let us take another look at Events, Triggers and Event Data via the diagram shown below:

We can summarize the following:

  • Events : They occur in Google Cloud Platform Services E.g. File Uploaded to Storage, a Message published to a queue, Direct HTTP Invocation, etc.
  • Triggers : You can chose to respond to events via a Trigger. A Trigger is the event + data associated the event.
  • Event Data : This is the data that is passed to your Cloud Function when the event trigger results in your function execution.

Currently supported Event Providers

At this point, Google Cloud Functions supports the following Event providers:

More events / services should be supported over time.

Types of Google Cloud Functions

There are currently two types of Google Cloud Functions that are supported.

  • Foreground functions (Synchronous) : These are directly invoked via a HTTP endpoint that is provided for your function. They are also known as HTTP Triggered functions. We will see an example of how to write HTTP Trigger based functions later on in the series.
  • Background functions (Asynchronous) : These are indirectly invoked via an event that triggers your function. Currently only Google Cloud Storage and Pub/Sub events are supported. Hence they can be referred to at times as Pub/Sub or Cloud Storage Trigger based functions. We will see an example of how to write Background Cloud Functions.

The diagram below summarizes this:

Event Object

Finally, we will touch upon the Event Object. This is the object that is passed to your function. The object depends on the type of event that is fired. There is a different schema for each type of event and

  • HTTP: ExpressJS Request in the case of JavaScript functions i.e. if you are using Node.js runtimes (either v6.x or v8.0) or a flask.Request object in case of the Python runtime.
  • Google Cloud Storage: Cloud Storage Object
  • Google Cloud Pub/Sub: PubsubMessage

We shall cover these when we cover foreground and background functions based on each of these triggers.

This concludes our overview of Google Cloud Functions. We can now move to setting up our environment and writing our first Cloud Function.

Proceed to the next part : Write your first Google Cloud Function or go back to the Google Cloud Functions Tutorial Home Page.

My passion is to help developers succeed. ¯\_(ツ)_/¯