[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

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

Hello.
I'm Mandai, in charge of Wild on the development team.

Are you familiar with GCP's Cloud Functions?
AWS Lambda for GCP is a serverless computing environment that allows you to easily prepare an execution environment just by deploying code.

It is very useful when you want to run a small piece of code without having to prepare a server, or when you want to quickly run a process that connects services that would be difficult to use with Cloud Pub/Sub alone. It is impossible to upload without it.

Furthermore, if you are responsible for important processing related to actual operations, a development environment is even more necessary.
However, I felt like there was no such thing as information on a development environment for implementing Cloud Functions, so I would like to create an environment that is easy to develop.

Prepare the editor

I recommend VSCode, which natively supports JavaScript

 

Prepare the module

Build an environment to run code for Cloud Functions locally.
However, all you need is one module provided by Google.

npm install @google-cloud/functions-framework -g

 

*2021/01/13 The global installation option was missing, so I added it.

 

With this single module, you can create a web server that behaves like Cloud Functions.

 

Prepare the project

Cloud Functions allows you to load the required modules through package.json.
Therefore, make the necessary modules readable via npm init.

npm init # Enter appropriate answers to questions about the project

 

Create a git repository

Personally, I think it's better to manage scripts with git, no matter how simple, so I'll use git init for now.

Cloud Source Repositories for your repository is convenient because it integrates the steps up to deployment.
As of January 2020, there is a limit of 5 users per month, 50 GB capacity, and 50 GB transfer amount, but I think it will fit within the free limit for trial use.

 

write the code

Cloud Functions scripts require an entry point.
The following is a minimal script with main as the entry point.

 

template

exports.main = (req, res) => { res.status(200).send('test'); }

 

In production, you will specify main as the "function to execute."
Next, let's run it locally and check how it works.

 

local execution

When running locally, run the following command on the command line to start the web server and wait for requests.

functions-framework --target=main

 

--target option is required and specifies the entry point.
If there are no errors, the web server is running on localhost:8080, so try running it with curl.

curl localhost:8080

 

If the above command returns "test", it is successful!

This command can be converted into an npm script and configured as follows.

  "scripts": { "start": "functions-framework --target=main" },

 

By adding the above settings, you can run it with npm start.

 

hot reload

With npm start, you need to manually restart the function-framework every time you change the code.
Since it is a fairly time-consuming task, I would like to introduce hot reloading.
We will introduce a module called npm-watch.

npm install npm-watch

 

It looks like a wrapper module for nodemon, but since no special settings are required, it was easy to install.

Add npm script.

  "scripts": { "start": "functions-framework --target=main", "watch": "npm-watch start" }

 

Next, add code monitoring settings.

  "watch": { "start": "*.js" }

 

Add the watch key at the top level (same level as main and scripts).

It's a bit complicated, but the command to start it is as follows.

npm run watch

 

If you do this, npm run start will be executed, but functions-framework will be restarted automatically if any changes are made to the underlying JS files.

The package.json configured so far 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 GCP Cloud Functions scripts.
Using this module, I was able to easily try out the behavior of Cloud Functions locally.
When adjusting or testing scripts, restarting functions-framework can be a hassle, so it's even better to build a hot reload environment using npm-watch to make your work more efficient!

lastly

I have opened the system development service site "SEKARAKU Lab" to which I belong.
Beyond is a one-stop service for everything from server design and construction to operation, so if you have any trouble with server-side development, please feel free to contact us.
SEKARAKU Lab: [https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)

That's it.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
9,547
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.