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

table of contents
Hello,
I'm Mandai, the Wild Team member of the development team.
Have you heard of GCP's Cloud Functions?
It's a serverless computing environment that allows you to easily create an execution environment by simply deploying code. It's 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
A development environment is even more necessary when it comes to handling important processes related to actual operation.
However, I felt like there was no information on the development environment for implementing Cloud Functions, so I would like to create a development environment that is easy to use.
Preparing the editor
I recommend VSCode , which supports JavaScript natively
Preparing the Module
We will create an environment to run Cloud Functions code locally.
At a minimum, all you need is a single 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 required modules through package.json ,
so make sure the required modules are readable 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
Cloud Source Repositories for your repository is convenient because it unifies the steps up to deployment.
As of January 2020, there are restrictions of up to 5 users per month, 50 GB of storage, and 50 GB of transfer volume, but I think that for trial use it should fit within the free tier.
Write the code
Cloud Functions scripts require an entry point.
A minimal script with main as the entry point looks like this:
template
exports.main = (req, res) => { res.status(200).send('test'); }
In production, you will specify main as the "function to run."
Next, let's run it locally and check that it works.
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 to specify 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
If you use npm start, you need to manually restart function-framework every time you change the code. This
is a rather time-consuming task, so I'll try introducing hot reloading. I'll introduce
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 functions-framework, which is useful when implementing scripts for GCP Cloud Functions.
Using this module, we were able to easily test the behavior of Cloud Functions locally.
Since restarting functions-framework can be cumbersome when adjusting or testing scripts, it's a good idea to set up a hot-reload environment using npm-watch to work more efficiently!
lastly
I am a member of the system development service site "SEKARAKU Lab."
Beyond offers a one-stop service for everything from server design and construction to operation, so if you have any problems with server-side development, please feel free to contact us.
SEKARAKU Lab: [https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)
That's all
0