Type to SearchView Tags

Understanding uses and advantages of Google Cloud Functions
Maneesh Tripathi Senior Technical Lead | May 11, 2020

Google Cloud Functions

Google Cloud Functions is an execution environment that enables users to deal with server less programming on top of Google Cloud Platforms. It is easy to run and can scale your code on the cloud environment. By using Google Cloud Functions, you can write simple, single-purpose functions that are attached to events/actions on your cloud infrastructure or services. Google Cloud Functions can be written in python, node.js and Go languages, so a developer need not learn any new languages.

Stateless Functions

Cloud Functions implements the server less paradigm where you just run your code without worrying about the underlying cloud infrastructure, such as servers or virtual machines. To allow Google to automatically manage and scale the functions, they must be stateless—one function invocation should not rely on in-memory state set by a previous invocation.

Google Cloud Functions Advantage:

  1. Event-Driven: We can write event-based Google Cloud function for any cloud events happen on GCP like File uploaded on Cloud storage or Google Firebase changes etc.
  2. Automatic Scaling: Developers need not plan the capacity for servers as it gets scaled up and down depending on increase or decrease of requests.
  3. No Server Management: Runs on the Google cloud infrastructure, so all H/w & S/w requirement is taken care of by Google.
  4. Pay what you use: This is a pay-per-use Google Cloud Platform.
  5. Integrated Services: Integration with other Google Cloud Platform services such as GCP, Google Firebase, and Google Assistance.

There are two types of Google Cloud Functions

  1. Http Function

You invoke HTTP functions from standard HTTP requests. These HTTP requests wait for the response and support handling of common HTTP request methods such as GET, PUT, POST, DELETE, and OPTIONS. When you use Cloud Functions, a TLS certificate is automatically provisioned for you, so all HTTP functions can be invoked via a secure connection.  An example of Http post function is stated below.

The following command shows how to call the function and pass it as a parameter using curl: curl -X POST HTTP_TRIGGER_ENDPOINT -H "Content-Type:application/json”'{"name":"Jane"}'where HTTP_TRIGGER_ENDPOINT(https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME

Image Source:  https://cloud.google.com/functions/

  1. Background Event-Driven Function

Google background functions can be created when you want to have your cloud function invoked indirectly in response to an event, such as a message on a cloud pub/bub topic, a change in cloud storage bucket, or a Firebase event.

Example: When any new file uploaded on Google cloud storage, a new log is written by google function using cloud event:

Image Source:https://cloud.google.com/functions/ 

Creation of Google Cloud Functions

This can be created in two ways:

  1. Google Cloud console Web Interface
  2. Popular IDE like Visual Studio Code

Integration with Google Services:

  1. Can be authenticated with Oauth and Google service account
  2. Can access all the Google Services such as Datastores, Storage, Firebase, and Google Assistance.

Uses of Google Cloud Function

  1. Integration with third-party services and APIs: Use Cloud Functions to surface your own micro services via HTTP APIs or integrate with third-party services that offer web hook integrations to quickly extend your application with powerful capabilities such as sending a confirmation email after a successful Stripe payment.
  2. Server less mobile backend: Use Cloud Functions directly from Firebase to extend your application functionality without spinning up a server.
  3. Server less IoT backend: Use Cloud Functions with Cloud IoT Core and other fully managed services to build back ends for the Internet of Things (IoT) device telemetry data collection, real-time processing, and analysis. Cloud Functions allows you to apply custom logic to each event as it arrives.
  4. Virtual assistants and conversational experiences: Use Cloud Functions with Google Cloud Speech API and Dialogflow to extend your products and services with voice and text-based natural conversational experiences that help users get things done. Connect with users on the Google Assistant, Amazon Alexa, Facebook Messenger, and other popular platforms and devices.
  5. Compute CPU intensive tasks on cloud instead of your mobile app: Developers can take advantage of Cloud Functions to offload the Google cloud resource-intensive work (heavy CPU or networking) that wouldn't be practical to run on a user's device. For instance, you could write a function to listen for image uploads to Google Cloud Storage, download the image to the instance running the function, modify it, and upload it back to Storage. Your modifications could include resizing, cropping, or converting images

Image Source:https://cloud.google.com/functions/

Google APIs support Google Cloud Functions 

Google cloud functions can work directly on many of the Google Cloud Platform offerings such as Google Data Store, Storage, Cloud SQL, Big Query, Big Data, and others. Anyone can use Cloud Function and leverage the services without setting up any server and environment with the help of Http calls, Cloud Storage, Cloud Pubs/Subs, Firebase or Stackdriver logging.

Use cloud function and compute CPU intensive tasks on cloud instead of in your Mobile app

Find more details on: https://cloud.google.com.

Image Source:  https://cloud.google.com/functions/