If you want to skip straight to the code, check out my Github for the full source code used in this article.

At the core of Google Cloud’s serverless offering is Google Cloud Functions. This Google Cloud product allows you to forget about worrying about provisioning servers, configuring auto-scaling, etc. In this article we will look at how we can quickly and easily create micro-services using Google Cloud Functions, Node.js and Express.

Prerequisites

Before starting you will need to ensure that you have installed Node on your machine, you have created a Google Cloud Project and you have installed the Google Cloud SDK.

To verify that Node has been installed run the following commands. You should see output of the Node version that you have installed.

npm -v

node -v

You can verify that the Google Cloud SDK has been installed with a similar command.

gcloud --version

Setting up Google Cloud

Once you have created your Google Cloud project we can enable the Cloud Function API. In the Google Cloud console navigate to APIs & Services and then in the library search for the Cloud Function API. You should find yourself on a screen similar to below. Go ahead and enable the API.

You may also have seen on this screen the free tier for Cloud Functions. This will be more than enough for us to be able to create our own micro-service as part of this article.

To verify that you are ready to start building and deploying your Cloud Function navigate to the Cloud Functions page in the console you should see that you are able to create a Cloud Function.

Creating the Express App

To initialise the app we are going to use the npm tool to create the package.json for us and then we are going to create the file that we are going to write our code in ourselves.

Alternatively, you could use the Express generator tool. This will create the skeleton of the project and is the quickest and simplest way to create an Express app. However, this generates a lot of files that we don’t need for this article.

Navigate to a new folder in your terminal and run the following command.

npm init

Follow the on-screen prompts and hit return line to skip the questions. You should now see that you have a package.json in your directory.

Next we will install Express and save it to our package.json .

npm install express --save

Finally, we will create our index.js file. This will be where we write our code.

nano index.js

Now we are ready to start coding. Open up your favourite code editor and we can get started.

Add the following code to the index.js

You can now run your app with:

node index.js

And you should now see Server running on port 5555 in the terminal. Congratulations! You have created your first Express app! The only issue is that if you go to http://localhost:5555 you won’t see anything because we haven’t configured the app to listen for requests.

Let’s modify our index.js with the following code to create some endpoints:

Run the app again and you should be able to go to http://localhost:5555/users and see our list of users.

Our second endpoint demonstrates how you can use path parameters in Express so if you go to http://localhost:5555/users/2 you will see our user “Jane Williams”.

Deploying to Cloud Functions

Now let’s deploy our app to Cloud Functions. To deploy our app we only need one command. In the root level directory of our app (the one with index.js in) run the command:

gcloud functions deploy first-function --runtime nodejs8 --trigger-http --entry-point app

This will take a few minutes to run but after it has finished you should see the following printed in the console.

Deploying function (may take a while - up to 2 minutes)...done.

httpsTrigger:

url: https://us-central1-<GCP-PROJECT-ID>.cloudfunctions.net/first-function

labels:

deployment-tool: cli-gcloud

name: projects/<GCP-PROJECT-ID>/locations/us-central1/functions/first-function

runtime: nodejs8

Let’s breakdown that command that we ran briefly. after gcloud function deploy the next parameter is the name of your function. The --runtime flag tells GCP which runtime your app needs, you should check your version of node and make sure that you use the appropriate runtime. The --entry-point flag points to the export in the index.js that is the entry point for the function. More information on the gcloud command can be found here.

After you have deployed your function you should be able to see it in the Google Cloud console and you can call your function by going to https://us-central1-<GCP-PROJECT-ID>.cloudfunctions.net/first-function/users

And that is it! We have successfully created and deployed our first micro-service on Google’s Cloud Functions.

You can find the complete source code from this article in my Github.

Happy coding!