Build a Google Cloud Function in Node

Build a Google Cloud Function in Node

tudip-logo

Tudip

20 September 2019

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.

Cloud Functions can be written using JavaScript, Python, or Go runtimes on Google Cloud Platform. You can take your function and run it in any standard Python 3 (Python 3.7), Node.js (Node.js 6, 8 or 10), or Go (Go 1.11) environment.

google-cloud-funcitons-1024x583

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.

Events-and-triggers

Serverless

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.

Use cases

Following table describe the use cases of Cloud Functions:

 

Use case Description
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.

express-node

Create a function

  1. Go to the console and select Cloud Functions.
  2. Click Create function.
  3. Enter the Name of the function.
  4. In the Trigger field, select HTTP.
  5. In the Source code field, select Inline editor. In this exercise, you will use the default function provided in the editor.
  6. Use the Runtime dropdown to select the Node.js 8 runtime.

Deploy the function

  1. At the bottom of the page, click Create.
  2. After clicking Create, GCP Console redirects to the Overview page.

Test the function

  1. Display the menu for your function by clicking 3 dots, and click Test function.
    test-function-1024x434
  2. Click Test the function.
    The Output screen displays “Hello World!”

search
Blog Categories
Related Blogs

None found

Request a quote