Google Cloud Function NodeJS Tutorial

Google Cloud Functions is the Serverless solution offered by Google Cloud Platform. It is a competitor to AWS Lambda functions. Try this tutorial if you want to quickly setup and start your Google Cloud Function in NodeJS. The only assumption is that you have a working GCP account.

Create a new Cloud Function

In the GCP console, select Cloud Functions, or in the bar at the top.

New Cloud Function

You should now see a list of your existing Cloud Functions. Click Create Function to Add a new one.

Create new cloud function

Configure my New Function

Now, let’s review our Clud Function’s key configuration parameters.

Main configuration

cloud function main parameters

Name is my function’s name. It should be unique for the selected project, and start with a letter.

Memory allocated is the allocated memory for my function’s execution. Minimum is 128MB, and maximum is 2GB. It should be enough for most of the standard use cases. If you think you may need more, then consider other solutions like App Engine or Compute Engine.

Trigger can be HTTP or other GCP triggers. Most used are HTTP for a Web API, or Clouod Pub/Sub for event based solutions. Cloud Storage can be used for computing based on events triggered by files.

Check Allow unauthenticated invocations if you want your deployed API to be accessed publicly.

Source Code

cloud function source code selection

Inline Editor is the most simple to start with. The code will be immediatly accessible via an online editor. But this solution is less viable if you work in teams or need industrial coding/deployment pipelines.

Cloud Source repository is for getting the code from a Source Code Repository (GIT).

Runtime

Select you function’s Runtime Environment

cloud function runtime selection

Go is famous for its performances and good design for concurrency.

Node.js is a JavaScript runtine. Probably the most documented and widely used.

Python is known for its rich ecosystem for Data Science and Machine Learning tasks.

I’d advice to pick the runtime based on your skills, or to go with Node.js by default. If you come accross use cases where you need Python or Go, you will know it by then.

Main file – index.js

This is the main file with the code which will be executed for each request received. In our case, it will simply output the message sent in the query. Or the message in the body. Or ‘Hello World!’ if both are missing.

Dependencies – package.json

This file contains our application’s package name (sample-http), version (0.1.1), and all our needed NPM dependencies.

Executed function

select cloud function to execute

Th function to execute. As specified in the index.js file.

Other Configuration

cloud function other useful configuration

Select your Region, matching where your clients may be if you want more efficiency. Be careful because region cannot be changed (The function should be created again).

If the function has not terminated in the specified timeout, it will be terminated. Be aware that the GCP fixed limit for this is 540s.

Cloud Function Monitoring

Once created, my function can be monitored in its differents states.

Creation in Progress

The rotating blue circle indicates that the creation is in progress, and it should take seceonds or at least minutes for the creation to be performed.

Cloud Function State

In this screen we can all my function’s main parameters.

The green icon to alert on the status (Up/Down), the name, region, runtime…

Last deployed date is useful when the function is down to see since when it failed for example.

Test my Function

The function should be correctly deployed now, but it is up to us to test it out.

cloud function test function

Select Test function

With the browser

The easiest way is to visit the function’s URL. It is unique and follows this pattern : https://{region}-{project}.cloudfunctions.net/{name}

For example : https://europe-west1-myproject.cloudfunctions.net/myFunction

The result in the browser should be :

cloud function result in browser

With the testing interface

In the function’s detail, use the TESTING tab

cloud function testing in the interface

View the Logs

Select View logs to access your function’s logs

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *