Cloud Functions

In this post, I want to take a look at Cloud Functions. It’s still in Alpha but you can already play with it and I really like the idea of deploying functions without having to worry about the underlying infrastructure.

What are Cloud Functions?

In a nutshell, Cloud Functions enable you to write managed functions to respond to events in your cloud environment.

  • Managed: Cloud Functions are written in JavaScript and run on a fully managed Node.js environment on Google Cloud Platform. No need to worry about instances or infrastructure. Just deploy your function to the cloud and you’re good to go.
  • Events: Cloud Storage and Cloud Pub/Sub events or simple HTTP invocation can act as triggers to cloud functions.
  • Response: Cloud functions can respond in async (with Storage and Pub/Sub events) or sync (with HTTP invocation) fashion.

Writing Cloud Functions

  • Cloud Function are written in JavaScript as Node.js modules.
  • Each function must accept context and data as parameters and must signal completion by calling one of context.success, context.failure, and context.done methods.
  • console can be used to log error and debug messages and logs can be viewed using gcloud get-logs command.

Deploying Cloud Functions

Cloud Functions can be deployed using gcloud deploy from 2 locations:

  1. Local filesystem: You can create your function locally and use gcloud to deploy it. (One caveat is that you need to create a Cloud Storage bucket for gcloud to store your function before it can deploy it.)
  2. Cloud Source repository: You can put your function to Cloud Source repository, (A Git repository hosted on Google Cloud Platform) and deploy it from there using gcloud.

Triggering Cloud Functions

Cloud Functions can be triggered (async or sync) in 3 ways:

  1. Cloud Pub/Sub: A new message to a specific topic in Cloud Pub/Sub (async).
  2. Cloud Storage: An object created/deleted/updated in a specific bucket (async).
  3. HTTP Post: A simple HTTP Post (sync). (This requires an HTTP endpoint in Cloud Function and this endpoint is created by specifying –trigger-http flag during deployment of the function.)
    
    

Resources