Anatomy of a Google Cloud function [Infographic]

Guy Dumais
Guy Dumais
Cover Image for Anatomy of a Google Cloud function [Infographic]

An infographic about the skeleton of a Google Cloud Function, one of the many by-products of the Google Cloud Platform family.

I've created this infographic for my personal learning and understanding first but I tought that it might be useful for anyone who wants to quickly understand the Google Cloud Functions.

Also, developers will find basic code examples in Javascript to create a Google Cloud Function (GCF) powered by Node.js and Express.js.

Anatomy of a Google Cloud function infographic

What is a Cloud Function?

In short, Cloud Functions, also known as Function-as-a-Service (FAAS), are web functions that can be called through HTTP requests. They are mainly used to automate internet processes. For example, a function can send a message to another server or system to start a task following an action from a new client on the web. Another use case could be to create a function as an authentication server for a headless website.

Of course, we can accomplish the same kind of tasks using REST/API functions executed in a web application on its own web server but the main difference here is that there is no web server to manage since the functions are powered by App Engine or Compute Engine server instances. This makes it a more economical service billed on a per-use basis. For my part, Compute Engine is more economical than App Engine.

The possibilities are limited only by the human imagination. In my case, I've created a Cloud Function to allow Analytics to register visitors who are using ad blockers in their browsers. So I don't miss any visit unless the user completly disable Javascript which is mandatory nowadays.

A Google Cloud Function could be an HTTP Function or a Background function, which will be explained a bit more details further down.

V8

Google Cloud Functions is built on a solid software infrastructure based on the Google V8 engine. In short, V8 allows you to interpret JavaScript code (ECMAScript) on any server (Windows, Mac, Linux, etc.). Google developed this engine in 2008 at the same time as Google Chrome to allow Chrome to run on different platforms (Android, Windows, Mac).

The V8 runtime provides rules for how memory is accessed, how the program can interact with the computer's operating system, and what program syntax is legal. Each web browser has a runtime environment for JavaScript.

Node.js

The second software component of a Google Cloud function is Node.js,, an asynchronous server software that processes HTTP requests in parallel rather than synchronously or sequentially like an Apache server. This is what allows it to process thousands of HTTP requests at the same time and easily absorb spikes in web traffic without overloading the server. To achieve this feat, Node.js uses a principle of request processing called Non-blocking I/O which, in short, allows the server to process other web requests while being busy performing slow operations that monopolizes the processor. in the case of a synchronous architecture.

Google Cloud Function uses version 10 of Node.js which is based on the ES6 language and supports some elements of ES7, ESNext. For example, here is the ES6 code for Node.js to run a web server that responds to HTTP requests on port 3000 of a local computer:

Loading...

Express

The third software component of a Google Cloud function is Express which simplifies the coding of web applications under Node.js while respecting good asynchronous coding practices. To create the same HTTP server as in the previous example under Express, all you need is to use the following code:

Loading...

HTTP function

With a Google Cloud Function, you don't need to create a server to listen to requests. Instead you create an HTTP Function, the entry point of the Cloud Function, written in Javascript with the following signature:

Loading...

This code comes from my Google Analytics Function to bypass Ad Blockers. As you can see, there is no code to run a server but instead just a function signature exported with a Request and a Response parameters.

It means that the function is always ready and waiting for requests to comes in and all you have to do is validate and process them the same way you would do it with a regular Node.js server.

Background function

You can also use background functions when you want to have your Cloud Function invoked indirectly in response to an event, such as a message on a Pub/Sub topic, a change in a Cloud Storage bucket, or a Firebase event.

Dependencies

You can also add the same external modules available on NPM as you would use within a Javascript web application by creating a package.json file. For example, here is the package.json file I'm using for my Google Analytics Cloud Function:

Loading...

In the code above, I added the node-fetch module which is installed automatically by Google from NPM.

Conclusion

Hoping this article has been helpful to better understand what are the Google Cloud Functions and especially that it has allowed you to better see the possibilities to expand your web applications.

I will be posting a complete tutorial on how to build a Google Analytics function to register visits from browsers using Ad Blockers and another tutorial about a complete authentication system using OAUTH 2 and MongoDB.

See You!


Comments:

    Share: