Google Cloud Functions Tutorial : Setting up a Local Development Environment
This is part of a Google Cloud Functions Tutorial Series. Check out the series for all the articles.
In this post, we shall take a look at setting up a local development environment for Google Cloud Functions.
The set of tools that we shall deploy will help us in multiple areas like development, managing our Cloud Functions deployment, Local Testing and Debugging and more.
It is advisable that you have the following tools setup to enable you to make best use of all the posts present in this tutorial series since some of the posts later on in the series will assume that you have these tools installed.
Keep in mind that at the time of writing, Google Cloud Functions support multiple runtime environments that allow you to write your Cloud Functions in JavaScript, Python or Go (Alpha). We are using JavaScript in this tutorial and depending on Node.js v6 or Node.js v8 that you prefer, you will need support for that in your local environment.
Google Cloud Platform Project Setup
The first step is to ensure that you have a Google Cloud Platform account with Billing setup. Remember that there is an always free tier when you sign up for Google Cloud and you could use that too.
The Always Free tier in Google Cloud Platform is extremely generous when it comes to Google Cloud Functions. Take a look at the following:
- 2 million invocations per month (includes both background and HTTP invocations)
- 400,000 GB-seconds memory, 200,000 GHz-seconds of compute time
- 5 GB network egress per month
Not bad, isn’t it ?
Optional: If you have not created a Google Cloud Platform project, I suggest to create a new Cloud Project under which you can have all the examples covered in this series. Create the New Project from the Cloud Console and fill out the details for your project. A sample screenshot is shown below.
Please note down your project-id, which is unique across all Google Cloud Platform projects
Once you have setup your project, the next step is to enable the Google Cloud Functions API for your project. From the Cloud Console, click on the Main Menu in the Top Left corner and navigate to Menu → APIs and Services → Dashboard as shown below:
Click on Enable APIs and Services. This will bring up a screen as shown below, where you can enter Cloud Functions in the search field.
Click on Google Cloud Functions API and then click on Enable. This will enable the API for use in your Google Cloud Platform project.
Development / Deployment Diagram
Let us take a look at an overall diagram that explains the tools that we will use across this set of tutorials:
You would be familiar with the Google Cloud Console (Web UI) that you use to manage services across Google Cloud Platform. In addition to using that, we can use the gcloud SDK , that we can deploy on our local machine to both deploy/monitor/manage our Google Cloud Functions directly. Finally, we setup our Local Development Environment in the next few sections.
gCloud SDK Installation
Google Cloud SDK is a set of tools to interact with various Google Cloud Platform Services. It is available to use from the Command Line (CLI).
Go ahead and install this via a download from https://cloud.google.com/sdk/ . Choose a download for your platform and go ahead with the standard setup.
Once installed successfully, you can try out the functions
component via some sample calls. Google Cloud Functions are available via gcloud functions
command.
Try the following to list out the Google Cloud Platform regions in which Cloud Functions is currently available:
$ gcloud functions regions listNAME
projects/mindstormclouddemo/locations/europe-west1
projects/mindstormclouddemo/locations/us-east1
projects/mindstormclouddemo/locations/us-central1
projects/mindstormclouddemo/locations/asia-northeast1
Now that we are set with gCloud SDK, we need to configure our Google Cloud Platform Project to use with the gCloud SDK. To do that, you need to execute the following command:
Do a login first via:
$ gcloud auth login
Finally, set the project id via the following command. Please use your project id instead of the PROJECT_ID value below.
$ gcloud set project PROJECT_ID
Finally, validate that you are logged in with your project and the project id is also correctly setup via the following command:
$ gcloud config list
Check the account
and project
values and ensure that they were set correctly.
Local Development Setup
For your local development environment, you will need to have the Node and npm tools setup. Please install them from https://nodejs.org/en/download/ and install it for your environment.
On my machine, I have the following setup:
$ node --version
v8.9.3$ npm --version
6.4.1
Note: At the time of writing, a couple of Node.js runtime versions are supported on Google Cloud Functions : v6.14.0 and v8.11.1 , so you should make sure to have the appropriate Node.js version on your local machine.
Once you have Node.js installed, you need to setup the Local Functions Emulator. This is a tool made available by Google to help you test your functions locally without the need to deploy to the live environment every time. We will cover this in a post later in the series. For now, install the Local Functions Emulator via the command given below:
npm install -g @google-cloud/functions-emulator
Finally, I have Visual Studio Code on my machine. It is a great and fast IDE that helps me develop and test stuff locally. I recommend it but you are free to use an IDE of your choice.
That completes our setup for our local development and management of Google Cloud Functions.
Proceed to the next part : Writing Background Cloud Functions or go back to the Google Cloud Functions Tutorial Home Page.