Introduction to Google Cloud Functions

Reading Time: 4 minutes
Data Pipeline in GCP: Cloud Function Basics | by Mostafa Varzaneh | Towards  Data Science

What are Google Cloud Functions?

 Google Cloud Functions is a Function as a Service (FaaS) that allows engineers and developers to run code without worrying about server management. Cloud Functions scales as needed and integrates with Google Cloud’s operations suite (such as Cloud Logging) out of the box.  

Functions are useful when you have a task or series of tasks that need to happen in response to an event. Cloud Functions can be invoked from several events, such as HTTP, Cloud Storage, Cloud Pub/Sub, Cloud Firestore, Firebase, and in response to Google Cloud Logging events. With Cloud Functions, monitoring, logging, and debugging are all integrated, and functions scale up and down as required. This is especially useful when you want to focus on writing code but don’t want to worry about the underlying infrastructure. You can write functions in the Google Cloud Platform console or write them locally and deploy using Google Cloud tooling on your local machine. 

How Google Cloud Functions works

GCF operates under the following three tenets:

  • Simplify the developer experience
  • Avoid lock-in with open-source technologies
  • Pay only for accumulated usage

Scalability is a major feature of GCF. As with other FaaS platforms, instances spin up and down as needed to accommodate changing workloads. If increased activity incurs more requests, GCF creates a new instance for each new request.

The Cloud Functions platform enables teams to craft functional, extensible applications that run code when triggered by preset conditions or events. The platform automatically manages all the computing resources required in those processes, freeing up DevOps teams to focus on developing and delivering features and functions. GCF also enables teams to run custom-written code to connect multiple services in Node, Python, Go, Java, .NET, Ruby, and PHP. These functions can connect with supported cloud databases, such as Cloud SQL and Bigtable.

GCF use cases

Cloud Functions are ideal for creating backends, making integrations, completing processing tasks, and performing analysis. GCF leverages webhooks, which facilitates connections to third-party services. Your functions can act as intermediaries between these hooks and APIs to enable automatic task completion. You might do this for payment alerts, GitHub commits, or in response to text alerts.

On the processing side, GCF functions can interface with Google’s own AI/ML technologies to inspect video and image content. Functions integrate with APIs to make this possible, forming a processing chain that eventually commits data to cloud storage.

GCF also has relevance in IoT and file processing tasks. Overall, these functions excel in scenarios where data enrichment and logic application are paramount.

Finally, it can help perform maintenance tasks or even offload on-device tasks to the cloud to conserve processing power.

GCF Workflow

When not to use Google Cloud Function

Like other FaaS platforms, GCF is not ideal for every situation. The platform is a standout when performing tasks aligned with Google’s suggested use cases — or when it makes clear business sense — but some situations may match better with other compute approaches.

For example, multi-function applications that are memory intensive may not be ideal for GCF if they scale aggressively. As with all FaaS offerings, maximum memory allocations are limited and can drive up costs for memory-intensive compute functions.

As with other FaaS platforms, GCF may not be ideal for infrequent, time-sensitive tasks. When a container cold starts — spins up for the first time to complete a new request — there is a slight delay in normal response time. At scale, these small delays can add up to precious seconds, which could impact end users and business outcomes. Workloads like these may be better served using a VM-based platform like GCE, or a specialized framework

Types of Google Cloud Functions

There are two types of Google Cloud Functions:

  • HTTP functions
  • Background functions

HTTP Functions

The HTTP functions are primarily used when you want to invoke your function via an HTTP(S) request. This type of function can be used for webhooks and creating APIs. The Cloud Function abstracts away the information infrastructure, so you do not need much code.

Executing your Google Cloud Function has to signal your function in order to actually run. This is known as a trigger. The HTTP trigger comes from a request sent to your function or can be an event from Google Pub/Sub events, stack driver, or a Firebase Event.

Background Functions

Background functions are triggered when specific cloud events occur. For example when a file is uploaded into the cloud, a function can automatically run to process the upload. These background functions currently respond to:

  • Pub/Sub events
  • Cloud storage
  • Firebase events

This creates multiple use cases such as:

  • Transforming data
  • Processing IoT messages
  • Watching project logs and reacting to specified changes within the infrastructure

Written by 

Sumit Agarwal is a Software Consultant having more than 1+ years of experience. He is good at problem-solving skills. He likes to watch football and cricket. He has mainly experience in Java and Mysql.