Serverless architectures have been gaining wide traction among developers over the last couple of years. They're being promoted among developers as a way to focus on their code while letting the infrastructure vendor take care of running that code for them. It's not that serverless paradigms haven't been there in the past. Platforms as a Service (PaaS) have been around for years now and while they do achieve the goals to a large extent, the recent serverless platforms released by large vendors have been significantly different both in terms of features, what the developers need to do, and how they're priced.
Amazon Web Services (AWS) has been off the blocks for a couple of years now with their AWS Lambda offering. The last six months have seen the design of multiple reference architectures around AWS Lambda with close Integration with other AWS Services. Microsoft has Azure Functions, its serverless offering, and IBM recently introduced its OpenWhisk serverless solution under the Bluemix brand. This article is a technical tutorial on getting started with Google Cloud Functions, the serverless offering on the Google Cloud Platform (GCP). Google Cloud Functions finally entered beta at the recent Cloud Next 2017, Google's annual cloud conference.
What is Google Cloud Functions?
High-level features of Google Cloud Functions are listed below:
- Support for turning existing logic into callable functions that can optionally exist for only as long as the logic is active.
- The billing occurs based on the number of invocations and the execution time, down to 100s of milliseconds. This provides immediate opportunities to organizations that seek alternative methods to autoscale while minimizing chances of overcapacity and any associated costs.
- Integrated with other GCP Services like Cloud Datastore, Cloud Pub/Sub, and more.
- Directly trigger Cloud Functions via a HTTP webhook. This is useful in triggering the Function from external services.
- Use Cloud Functions directly from Firebase.
Getting Started with Cloud Functions
Let's get started. Now, you'll write your first function with Google Cloud Functions and execute it.
Google Cloud Functions executes the function based on events. Events can be triggered as a result of a database update in Cloud Datastore, a message being published in Cloud Pub/Sub, a BLOB getting uploaded to Google Cloud Storage, an event from Firebase, and more. These are called background functions, where they'll be executed in response to an event, which acts as a trigger. We'll take a look at various triggers that Google Cloud Functions supports a bit later in the article.
Google Cloud Functions also supports a foreground function (HTTP invocation). For example, you can directly invoke this function via a HTTP URL that's unique to your deployed Cloud function. Let's take a look at that.
Before you start, assume:
- You have a GCP project
- You have enabled Project Billing
- From the API Manager, you have enabled the Cloud Functions API
Cloud Functions is available from the main menu on the left of the screen as shown below. You'll find it in the Compute → Cloud Functions section.
Once you click on that, you'll be taken to the Cloud Functions functionality, and if you haven't deployed any Cloud Functions previously, you should see a screen that looks as follows:
Click on Create function. This will take you to a form, where you can not only fill out the function details, but also use the inline editor to write your Cloud Function. Let's look at the form details in two parts, the first of which is shown below.
You'll provide a name for the function, use other defaults, and select the Trigger as HTTP Trigger option. Notice that the unique HTTP URL for your function is shown, too. Syntactically, it conforms to the following format:
The next step is to write the Source Code for your function in an inline editor, so select Inline Editor for Source Code option in the Create function screen. This will show a helloworld function template in the inline editor, as given below:
The inline editor is a convenient way to write and test out Cloud Functions but may not be the ideal way to write code in the long term. For that, you can use the source code editor of your choice (e.g., Atom, Sublime, etc.) and upload your source code as a ZIP file. You can also deploy it from the Cloud Source Repository, a private Git repository that is provided with each Google Cloud Platform project