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.
- 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
- 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:
- 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.)
- 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:
- Cloud Pub/Sub: A new message to a specific topic in Cloud Pub/Sub (async).
- Cloud Storage: An object created/deleted/updated in a specific bucket (async).
- 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.)