Want a two-word explanation of Google Cloud Functions?
That’s it! Action and reaction. That’s Google Cloud Functions in a nutshell. But of course, since this doesn’t capture all the ways this service is cool, let’s go into a bit more detail.
In this article, I’ll give you a more detailed overview of Google Cloud Functions, compare the 1st gen and 2nd gen environments, and discuss some good uses for the service. I’ll also explain how to deploy a simple Cloud Function.
Google Cloud Functions: An Introduction
Google Cloud Functions is a serverless compute platform that allows you to run code in response to events without having to provision or manage servers. Because Cloud Function is a fully managed service, it is a great way to efficiently automate tasks, build microservices, and connect your applications to other cloud products and services, both on and off Google Cloud. I like to think of Cloud Functions as the “glue” between services that allow you to extend the built-in capabilities to achieve your programmatic goals.
Cloud Functions falls into the Functions as a Service (FaaS) category of computing. FaaS is all about the code–and just the code. With Google Cloud Functions, you have your choice of working with a range of runtimes: Go, Java, .NET Core, Node.js, PHP, Python or Ruby. Here’s an example of a simple Cloud Function in Python:
This Cloud Function is triggered directly by an HTTP request, and represents one of two major types, the other being an Event. This is a 2nd gen Cloud Function (more about that in a bit) which makes it easy to include all the necessary libraries with a single import of the functions_framework. Then, the entry point or main function is registered with the chosen runtime framework. Because it is an HTTP triggered function, supported HTTP method types or verbs (such as GET, POST, and PUT) are referenced. Finally, all Cloud Functions must return a response.
Cloud Google Functions: 1st Gen compared to 2nd Gen
The initial release of Cloud Functions–now known as 1st gen–could be triggered directly by an HTTP request, as noted above, or an event from one of a limited number of Google Cloud Platform services, including Cloud Storage, Cloud Pub/Sub, and Cloud Firestore. Google Cloud completely re-architected Cloud Functions for their 2nd gen release. Built on a foundation of Cloud Run (Google Cloud’s serverless container computing product) and Eventarc, a platform-wide triggering service, Cloud Functions 2nd gen capabilities were greatly expanded.
Now, Cloud Functions can be triggered directly by 18 different Google Cloud services as well as through changes to Audit Logs covering over 90 products, and even a handful of 3rd-party services–as well as HTTP requests, by default. The 2nd gen revamping brought a doubling of computing power and memory available in addition to an increase of concurrent requests by a thousand-fold.
What are some good uses for Google Cloud Functions?
Cloud Functions can be put to use in a myriad of ways:
- Asynchronous microservices integration: Connect with 3rd-party APIs, pull data from mobile app backends, or enhance workflow automation.
- Operational streaming analytics: Gather info from IoT devices, perform video or image analysis, or send your massaged data to BigQuery for near real-time analysis.
Cloud Functions is particularly useful when working with AI APIs as with this workflow that takes an uploaded image, optically recognizes the text, translates it into five different languages and then stores the translations into separate documents:
How to deploy a simple Google Cloud Function
The Google Cloud Functions console allows you to create and deploy a Cloud Function quickly and efficiently. In fact, the creation process is identical to deployment.
Cloud Functions can also be deployed via the command line. Here’s the command for deploying a 1st gen HTTP function:
gcloud functions deploy FUNCTION_NAME --source=LOCAL_PATH --entry-point=CODE_ENTRYPOINT --trigger-http
Deploying a 2nd gen function, triggered by a audit log event such a Cloud SQL instance failover, is just as straight-forward:
gcloud functions deploy FUNCTION_NAME --gen2 \ --event-filters="type=google.cloud.audit.log.v1.written" \ --event-filters="serviceName= serviceName=cloudsql.googleapis.com" \ --event-filters="methodName= cloudsql.instances.failover" \ --service-account=PROJECT_NUMBERfirstname.lastname@example.org
Google Cloud Functions are an essential element of today’s microservice, cloud-based event architecture. Because they’re serverless, they scale effortlessly as needed to connect almost any declared action to your desired reaction.
Want to learn more? Check out the following resources: