Cloud Functions

Cloud Functions make it possible to run a completely serverless environment, where logic can be executed on demand and in response to events. They act as a glue between disparate applications.

With them, you can build a serverless microservices architecture that’s highly scalable and focus only on the code and not worry about setting up servers. They can be used in a variety of cases that require lightweight microservices, event-driven processing or webhooks.

GCP Services emit events such as when files are uploaded to a Cloud Storage bucket or messages are published to a Cloud Pub/Sub topic.

They can invoke APIs or write data back to the Cloud and they’re ideal for microservices that require a small piece of code to quickly process data related to an event.

They’re priced according on how long your function runs, the number of times it’s invoked and the resources that you provision for the function.

They can be called asynchronously to events or synchronously by direct HTTP requests. For HTTP functions, it’s important to keep the execution time to a minimum.

Cloud functions have a default timeout value of 60 seconds.

Example use case

Consider an application that allows users to upload images that contain text to a bucket.

  1. The OCR Cloud function is triggered when a new image is uploaded to the images bucekt. This function uses Google Cloud vision API to extract text from images, and then queues the text in Clodu Pub/Sub for translation.

  2. The translator Cloud function triggered by the topic invokes the translation API to translate the text. It queues up the translated text in the file-writer topic in Pub/Sub.

  3. The file-writer Cloud function writes the translated text for each image as a separate file in Cloud Storage.

This service uses fully managed services and APIs such as Cloud Storage, Cloud Pub/Sub, Cloud functions, Cloud vision API and Cloud translation API. These services scale automatically depending on the volume of incoming data and required compute resources.

Writing, Deploying and monitoring Cloud Functions

writing

You can write code for Cloud functions as Node.js functions in an Index.js file.

A background function can take an event and callback function as input parameters. An HTTP function takes request and response objects as input parameters.

deploying

There’s no need to upload zip files with package dependencies. You can specify any dependencies in a package.json file. The cloud function service automatically installs all dependencies befoer running your code.

Cloud functions provide access to a local disk mount /tmp that you can use for tmp file processing. Make sure to clean any tmp files when you’re done.

You can deploy a function from your machine by using GCP Console or the gcloud command. It automatically zips up your code and uploads it to the bucket that you specify.
You can also deploy code directly from source repositories external to Google such as GitHub or Bitbucket.

monitoring

You can view the output from your console.log and console.error messages and StackDriver logging.

If your code throws errors or in case of other uncaught errors, Cloud functions automatically captures these errors in StackDriver error reporting.

In GCP Console you can view metrics related to the number of invocations, execution time and memory usage for your functions.