How fine-grained level can you reach when it comes to application development? A virtual machine? A node? A container? Or maybe a single function?
Find out how to build and extend a product in the cloud quickly and at a relatively low cost using the Cloud Functions service.
But before we start, a few words about serverless
Serverless is an “invisible” infrastructure on which your code is run. You don’t even need to know what devices are underneath; you don’t have to bother about machine management, provisioning, OS updates, security patches, network management, or load balancing. All that work lies on Google’s side, and resource scaling is done automatically in response to current needs.
So, when your application is experiencing an increase in traffic, Google will scale the infrastructure up, and when the load decreases, the infrastructure will scale down. In some cases, when there is no traffic at all, the service can scale to zero. And because in the cloud you are charged only for the resources consumed in a pay-as-you-use model, zero traffic means zero costs.
Here you will find more information about serverless: What is serverless, and how does it work?
What is the Cloud Functions service?
Cloud Functions is a serverless environment for connecting cloud services and executing tasks. The service allows to write and deploy pieces of code that will be run in response to a particular event emitted by a GCP service or from outside the platform, using HTTP protocol and a unique URL. The code is triggered in a runtime environment fully managed by Google; the user does not have to provide or configure any infrastructure.
The service increases the level of agility by building applications consisting of small, independent units of functionality – modules. Each focuses on performing only one activity. Cloud Functions enable users to build and deploy services at the level of a single function rather than at the level of an entire application, container or virtual machine.
Cloud Functions programming languages
Currently (October 2022), Cloud Functions supports the following programming languages and runtimes:
If you have an application built in any of the above programming languages, you can quickly deploy it on the service without configuring or maintaining a server.
Types of Cloud Functions
There are two types of Cloud Functions:
- HTTP functions, which handle HTTP requests and use HTTP triggers. Use an HTTP function when you need your function to have a URL endpoint and respond to HTTP requests, such as for webhooks.
- Event-driven functions, which handle events from your cloud environment and use event triggers. Use this one when your function should be triggered directly in response to events within your Google Cloud projects, such as messages on a Pub/Sub topic or changes in a Cloud Storage bucket.
Triggers and cloud events
Cloud events are small actions that occur in cloud services. It could be a change to a database, adding a file to a storage service, or creating a new virtual machine.
Events occur regardless of whether users respond to them. You can create a response to given events using a trigger. A trigger declares that a cloud function interests a particular event or several. Combining a function with a trigger allows you to run the code and pass data to it about the event.
Extending and connecting cloud services
Cloud Functions provides a logic layer that allows customers to write code that extends and connects cloud modules. The service listens for events from the environment – for example, uploading files to the Cloud Storage service, logging changes or a new message in the Pub/Sub queue.
Cloud Functions authenticate with a service account (just as roles and permissions are granted to physical users, services accessing cloud resources also have accounts). Functions can operate on most GCP services – Datastore, Cloud Spanner, Cloud Translation API or Cloud Vision API. In addition, Cloud Functions are supported by many Node.js client libraries, which also facilitate integration.
Google Cloud Functions use cases
Serverless mobile backends
Combining Cloud Functions with the Firebase mobile app development platform allows you to extend the functionality of your app without having to run a server. This makes it possible to take care of user engagement by triggering functions in response to their actions or based on analytics data.
Video and image analysis
Cloud Functions can support the operation of the Video Intelligence API or Cloud Vision API services to analyse images and videos rapidly. This makes it possible to read information from multimedia, search for data or derive insights in an automated way.
The service can also be used to extend applications with voice and text-based services – for example, virtual assistants, chatbots or IVR systems. By combining Cloud Functions with Dialogflow or the Cloud Speech API and communication platforms (like Slack, Messenger, or Google Home, which send messages to the service via HTTP JSON requests), you will allow your users to get things done faster and more conveniently.
To sum up
Cloud Functions provide a layer of logic to connect and extend other cloud services. GCP users can write and deploy simple, usually one-off functions, which can be linked to events emitted by other cloud services. Google Cloud Functions allow services to be built and deployed at the level of a single module rather than an entire application, container or virtual machine, simplifying the product development and deploying process. Thanks to the serverless model, IT costs can be reduced, and more resources can be moved from the maintenance area to innovation development.