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.
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.
A Google Cloud Function could be an HTTP Function or a Background function, which will be explained a bit more details further down.
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:
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:
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.
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.
In the code above, I added the node-fetch module which is installed automatically by Google from NPM.
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.