December 25, 2019

Google Cloud Functions and the Beauty of Serverless Execution

If you are in cloud application development, you must have heard about serverless execution multiple times. Your manager or client must have said let's go for serverless and save some cost. Well, they are not entirely wrong. When you deploy your application on EC-2, Compute engine, AppEngine or Container engine, you create an instance, a dedicated machine that will run 24*7 and you will be billed accordingly. What if you have an option to Pay only while your code runs? It sounds cool, right? That's where Serverless comes into the picture. Major Cloud Service Providers have their Serverless Execution Environment. AWS has Lamda and Google Cloud has Cloud Functions. I will specifically talk about Cloud Functions here.

Cloud Functions

Cloud Function is Event-driven Serverless compute platform. The official Documents from google says cloud function provides a connective layer of logic that lets you integrate and extend GCP and third-party services, making it possible to rapidly build serverless applications that are highly available, secure, and cost-effective. It is event driven, automatically scalable and fault-tolerant. In simple terms, you just write the code and it can be triggered by events emitted from your cloud infrastructure and services or Rest APIs, and the interesting part is you will only pay for the time while your code runs. Here is a list of event types that are supported:

  • HTTP
  • GCS
  • PubSub
  • Stack driver
  • Firebase

The Supported Languages are:

  • NodeJs
  • Python
  • GO

How it Works?

I will take a reference from Google's official documentation to explain it.

Cloud Functions

  1. First step, one of your cloud service or HTTP Request (Supported event Mentioned above) will generate the events and when the event is fired it will trigger your cloud function
  2. Cloud function contains the actual code, it will execute the code on fully managed instance by Google.
  3. Once the Execution is done, you can write the output back to cloud services and trigger other cloud functions or you can use other API for further Processing.

Use Cases

Serverless application backends

Post a comment on Slack channel in response to a GitHub commit: Suppose you want to Post a Comment to your team's slack channel when one of your team member pushes the code changes in your Project's GIT repository.

  • Use Git Webhook to Generate Event when someone pushes the code in GIT Repository
  • Write a cloud Function and attach it to GIT Webhook event using REST endpoint
  • Use Slack Post Message API to push the message on Slack Channel

Cloud Functions
Real-time file processing

It's a very common use case. You may want your code to respond to events from Cloud Storage or Firebase Storage to process files immediately after upload completes to generate thumbnails from image uploads, process logs, validate content, transcode videos, validate, aggregate and filter data in real-time.


The other use case of Real-time file processing could be categorizing the data. As soon as an image or a video file arrives in cloud storage bucket and you may want to apply machine learning to categorize it either as a person(male/female), animal(cat/dog), object etc. You may also want to extract information like emotions from images such as smile, fear, angry or having beard etc. Here is how it will work:

  • Create a GCS Bucket , when file will be uploaded it will generate the event, event will contain the required information.
  • Write a cloud function and bind it to events generated from cloud storage bucket.
  • You can use google cloud machine learning Vision API to apply Machine Learning and Extract useful information.

Cloud Functions
Real-time stream processing

Use Cloud Functions to respond to events from Cloud Pub/Sub to process, transform and enrich streaming data in transaction processing, click stream analysis, application activity tracking, IoT device telemetry, social media analysis, and other types of applications. Here is how this can be done:

  • Your Application will push the message using pubsub push api on pubsub topic.
  • write a cloud function and bind it to events generated from pubsub., it will generate a event which will trigger your cloud function.
  • You can use google cloud machine learning API for Predictive analysis or Bigquery for Warehouse, Business intelligence and Data Analytics or other Google cloud service to store or process data as per your need.

Cloud Functions