Google Cloud Functions Tutorial : What is Google Cloud Functions?

  • 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 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:

  • 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:

Types of Google Cloud Functions

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

  • 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.

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

  • Google Cloud Storage: Cloud Storage Object
  • Google Cloud Pub/Sub: PubsubMessage

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store