Build a Google Cloud Function in Node
What are Google Cloud Functions?
Cloud Functions is a serverless running environment for building and connecting cloud services. With Cloud Functions, you can write single-purpose functions which are attached to an event emitted from your cloud services. Your function is triggered when an event being watched. Your code executes in a fully managed environment. There is no need to provision any servers or worry about managing any infrastructure.
Connect and extend cloud services
Cloud Functions act like a connective layer that lets you write code to connect and extend cloud services. Monitor a file upload to Cloud Storage, a log change, or an incoming message on a Cloud Pub/Sub topic. Cloud Functions have access to the Google Service Account credential and are authenticated with the majority of Google Cloud Platform services, including Cloud AutoML Vision, as well as many others. In addition to cloud, functions are supported by numerous Google Cloud client libraries, which further simplify these integrations.
Events and triggers
Cloud events are the things that happen in your cloud environment.
Realtime examples of events are:
- changes to data in a database
- Respond and listen to Cloud Storage events such as when a file is created, changed, or removed
- a new virtual machine instance created
Events occur whether you choose to respond to them or not. You create a response to an event with the trigger or you can say a trigger is a declaration that you are interested in a certain event. Binding a function to a trigger allows you to act on events.
Cloud Functions removes the work of configuring software, managing servers, updating frameworks, and patching operating systems. The infrastructure is fully managed by Google so that you just add the code. So the provisioning of resources happens automatically in response to events. This means that a function can scale from a few to many millions of invocations without any work from you.
Following table describe the use cases of Cloud Functions:
|Data processing / ETL||Listen and respond to Cloud Storage events such as when files are created, changed, or removed. Process images, perform video transcoding and invoke any service on the internet from your Cloud Functions.|
|Webhooks||By a simple HTTP trigger, respond to events originating from 3rd party systems like GitHub, Slack, Stripe, or from anywhere that can send HTTP requests.|
|Lightweight APIs||Compose applications from lightweight, loosely coupled that are quick to build and scale instantly. Your functions may be event-driven or invoked directly over HTTP/S.|
|Mobile backend||Use Google’s mobile platform for app developers, and write your mobile backend in Cloud Functions. Listen and respond to events from Realtime Database, Authentication, and Storage.|
|IoT||Imagine hundreds of thousands of devices streaming data into Cloud Pub/Sub, thereby launching Cloud Functions to process, transform and store data. Cloud Functions lets you do it in a completely serverless way.|
Cloud Function written in Node.js 8 Runtime:
Before creating a function you must prepare your local machine for Node.js development.
Follow the link for setting up a Node.js development environment.
Create a function
- Go to the console and select Cloud Functions.
- Click Create function.
- Enter the Name of the function.
- In the Trigger field, select HTTP.
- In the Source code field, select Inline editor. In this exercise, you will use the default function provided in the editor.
- Use the Runtime dropdown to select the Node.js 8 runtime.
Deploy the function
- At the bottom of the page, click Create.
- After clicking Create, GCP Console redirects to the Overview page.
Test the function
- Display the menu for your function by clicking 3 dots, and click Test function.
- Click Test the function.
The Output screen displays “Hello World!”