[GCP] Establishing an efficient Cloud Functions development environment [JavaScript]

table of contents
Hello.
I'm Mandai, the Wild team member in charge of development.
Are you familiar with GCP's Cloud Functions?
It's a serverless computing environment that allows you to easily set up an execution environment simply by deploying your code; it's essentially the GCP version of AWS Lambda.
It is extremely useful for running simple code that does not require the setup of a server, or for quickly connecting services that would be difficult to do using Cloud Pub/Sub alone. However, no matter how simple the code, it is impossible to upload it without testing it
Especially when dealing with critical processes related to actual operations, a development environment is absolutely essential.
Yet, I felt there wasn't much information available about development environments for implementing Cloud Functions... so I'd like to try creating a nice, easy-to-develop environment myself.
Preparing the editor
which natively supports JavaScript VS Code, I recommend
Preparing the Module
We'll set up an environment to run Cloud Functions code locally.
That said, the minimum requirement is just one module provided by Google.
npm install @google-cloud/functions-framework -g
*2021/01/13 Global installation options were missing, so they were added
This single module allows you to create a web server that behaves like Cloud Functions
Preparing the project
Cloud Functions allows you to load necessary modules through `package.json`.
Therefore, make sure that the necessary modules are accessible via `npm init`.
npm init # Enter appropriate answers to the questions about the project
Create a git repository
Personally, I think it's better to manage even the simplest scripts with git, so for now I'll just run git init
for your repository Cloud Source Repositories is convenient because it integrates the entire deployment process.
As of January 2020, there are limits of 5 users per month, 50 GB of storage, and 50 GB of data transfer, but I think you can stay within the free tier for trial use.
Write the code
Cloud Functions scripts require an entry point.
The following is a minimal script that uses `main` as the entry point.
template
exports.main = (req, res) => { res.status(200).send('test'); }
In the actual production environment, you will specify `main` as the "function to execute".
Next, let's run it locally and check its operation.
Local execution
If you are running locally, run the following command on the command line to start the web server and wait for requests
functions-framework --target=main
The --target option is required and specifies the entry point.
If no errors occur, the web server is running on localhost:8080, so try running it with curl or similar.
curl localhost:8080
If the above command returns "test", then it was successful!
This command can be made into an npm script and configured as follows:
"scripts": { "start": "functions-framework --target=main" },
By adding the above settings, you will be able to run it with npm start
Hot Reload
With `npm start`, you have to manually restart `function-framework` every time you change the code. This is a
tedious task, so I'm going to try implementing hot reloading. I'll
be installing a module called `npm-watch`.
npm install npm-watch
It looks like a wrapper module for nodemon, but since no special configuration is required, it was easy to get started with
Add the npm script
"scripts": { "start": "functions-framework --target=main", "watch": "npm-watch start" }
Next, add the code watch settings
"watch": { "start": "*.js" }
The watch key should be added at the top level (the same hierarchy as main and scripts)
It's a bit complicated, but the command to start it is as follows:
npm run watch
This will execute npm run start, but functions-framework will be automatically restarted if any changes are made to the JS files under it
The package.json configured up to this point looks like this:
{ "name": "test", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "functions-framework --target=main", "watch": "npm-watch start" }, "author": "y.mandai", "license": "ISC", "dependencies": { "@google-cloud/functions-framework": "^1.3.2", "npm-watch": "^0.6.0" }, "watch": { "start": "*.js" } }
summary
This time, we introduced the functions-framework, a useful module for implementing GCP Cloud Functions scripts.
Using this module, you can easily test the behavior of Cloud Functions locally.
Restarting functions-framework when adjusting or testing scripts can be cumbersome, so it's even better to set up a hot-reload environment using npm-watch to work more efficiently!
lastly
I have launched "SEKARAKU Lab," a service site for the system development company I belong to.
Beyond offers a one-stop service for everything from server design and construction to operation, so please feel free to contact us if you have any problems with server-side development.
SEKARAKU Lab:[https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)
That's all
0
