Serverless: Everything You Need to Know About Google Cloud Functions

Functions as a Service (FaaS) is one of the delivery models based on the serverless paradigm. It enables developers to write and execute code snippets with a well-defined entry point in response to an event. This invocation mechanism makes FaaS ideal for event-driven computing where the code is executed in response to an external event. To interactively trigger a function, an HTTP endpoint may act as a trigger. Developers can focus on writing fine-grained, autonomous, and stateless functions that may be assembled into an application. Each function may represent a microservice participating in the call sequence.
Google Cloud Functions is a serverless compute platform from Google which became generally available in August 2018. Along with virtual machines and containers, functions offer a mechanism to execute code in the cloud. This article provides a snapshot of the current state of Google Cloud Functions.
Concepts and Terminology
Cloud Functions provides a connectivity layer of logic that lets developers write code to connect and extend cloud services.
A function is a simple, single-purpose code snippet that is associated with events emitted from cloud infrastructure and services.
Cloud infrastructure and services raise events in response to a change in the state. For example, adding a new object to Cloud Storage raises an event.
Triggers connect events to Cloud Functions. Events happen irrespective of triggers and the existence of functions. But it is the trigger that maps an event to a function.
Functions may be invoked interactively through an HTTP endpoint. Background functions are invoked indirectly by a trigger. An event should take place before a background function is executed.
Execution Environment and Supported Runtimes
Google Cloud Functions is a stateless execution environment, which means that the functions follow a shared-nothing architecture. Each running function is responsible for one and only one request at a time. Because concurrent requests are processed by different function instances, they do not share variables or local memory.
If your function needs to maintain state, it needs to be externalized to services such as Google Cloud Memorystore, Cloud Datastore, Cloud Spanner, or Cloud SQL.
A function has access to the CPU and memory resources only for the duration of function execution. Code run outside of the execution periods is not guaranteed to execute, and it can be stopped at any time. It’s important that developers write code that marks the end of execution that guarantees a graceful exit.
A function’s execution time is limited by the timeout duration, which can be specified at the deployment time. A function times out typically after one minute, but this can be extended up to 9 minutes. When a function exceeds the execution timeout, an error status is immediately returned. Any remaining code that is executing might be terminated.
During the execution, a function can only write to /tmp location of the filesystem. All other directories including those that contain the files uploaded along with the function are read-only.
Google Cloud Functions provide an HTTP library compatible with ExpressJS (Node.js/JavaScript) and Flask (Python) for making outbound calls to the public Internet. Any connection that remains unused for two minutes might be closed by the system, and any further attempt to use a closed connection will result in an error.
By default, Google Cloud Functions use Node.js 6 as the runtime environment. Other supported runtimes include Node.js 8, Python 3.7.1, and Go 1.11, which are currently in beta. Node.js 6 is based on a Debian 8 OS, while Node.js 8, Python, and Go runtimes are built on top of Ubuntu 18.04. Developers can choose any of these runtimes during the creation of a function. By including package.json (Node.js) or requirements.txt (Python) files, additional libraries may be requested from the runtime. Cloud Functions will install those dependencies as a part of the deployment.
Events and Triggers
Google Cloud Functions can be invoked via the below events:
- HTTP (Direct invocation)
- Cloud Storage
- Cloud Pub/Sub
- Cloud Firestore
- Firebase
- Stackdriver Logging (Via Pub/Sub)
The data associated with the events is passed to the function as a parameter. For example, the header and body data is accessible through the request object. For background functions invoked indirectly by an event, there are special parameters called data and context that contain the details such as the source metadata, event identifier, timestamp, and event type.
Cloud Pub/Sub is one of the most powerful sources for Cloud Functions. Any external service can be easily wired to a Pub/Subtopic which will raise an event to trigger the function. Services such as IoT Core, Cloud Scheduler, and Stackdriver can be easily integrated with Cloud Functions through Cloud Pub/Sub.
The HTTP endpoint associated with Cloud Functions can be integrated with external webhooks. This is another powerful choice to connect third-party services to functions. Github webhooks and Google Assistant intents are classic examples of webhook sources for Cloud Functions.
Firebase deserves a special mention for its tight integration with Cloud Functions. Web and mobile developers who may not be using Google Cloud Platform can create and consume functions from Firebase. Similar to database triggers, a Firebase document can raise events when create, update, and delete operations take place. This brings the power of a serverless mobile backend to Firebase.
Developer Experience
Google Cloud Functions is available to developers through the Cloud Console and Cloud SDK. The popular command line interface, gcloud, can be used to manage functions. The platform is also integrated with the Serverless framework which provides a seamless mechanism to deploy and manage functions in multiple FaaS environments.
Cloud Functions Emulator makes it possible to test the code locally before deploying it to the cloud. Available as an NPM module, the tool only supports Node.js 6 deployments.
In the next installment of this series on Google Cloud Functions, I will show you how to integrate Cloud Functions with an IoT use case. Stay tuned!
Janakiram MSV’s Webinar series, “Machine Intelligence and Modern Infrastructure (MI2)” offers informative and insightful sessions covering cutting-edge technologies. Sign up for the upcoming MI2 webinar for a deep dive on Google Cloud Functions.