How to Deploy Cloud Functions with Node.js
About Cloud Functions
Google's Cloud Functions
let you run code and deploy to the cloud GCP with no servers or containers you need to maintain. In this blog we'll write our first cloud functions and deploy to the cloud.
GCP Account
First, you'll need GCP account, once you have already should be able to process the below steps.
Create Node.js function for Cloud Functions
This command will create package.json
file.
npm init -y
Then run this command to install the package @google-cloud/functions-framework
for testing cloud function locally.
npm install --save-dev @google-cloud/functions-framework
Create our first Node.js function index.js
, in side the index.js
file we'll add following code, simply just return the message hello.
exports.main = (req, res) => {
res
.status(200)
.send('hello');
}
How to Test Cloud Function in local
Add following section to the package.json
file's script section and run the npm run start
.
Now, should be see the URL: http://localhost:8080
, so that will host the code to our code exist at index.js
, since we specify the method is main
, so will run our first main
function and you should be see the hello
from the browser
"scripts": {
"start": "npx functions-framework --target=main [--signature-type=http]"
},
How to Deploy Cloud Function to the Cloud
After tested on your local, you're ready and want to deploy the function to the cloud, this section will show how to run the gcloud
command at the local environment and deploy to the cloud.
Config gcloud
Now, to run gcloud
command at local, you'll need to configure first. If you already config before, you can run the gcloud config list
to verify the account
and project
is point to the correct one, cause you don't want to accidentally deploy to the wrong GCP account.
If you have not setup before, you can run the gcloud init
to authenticate with GCP account.
Once confirmed you have config to the correct environment, you can run below command at the root folder your index.js
.
gcloud functions deploy my-first-function --trigger-http --region=us-central1 --runtime=nodejs16 --gen2 --allow-unauthenticated --entry-point=main
So, these options are pretty straightforward setting, gcloud functions deploy
is the command to deploy, my-first-function
will be the name of the functions shows on the Cloud Functions alter, trigger-http
mean this will works as HTTP(S) request, runtime
specify what's the version of the app during the build, entry-point
is the main function cloud function will run.
Also, gen2
is next version of the Cloud Functions, provide longer request processing and handler more regions and fast rollback.
At this point, if the above gloud functions deploy
successfully run, you should see the message on your console as done along with other information, such as build,label or the options you specified.
Also, the url will provide HTTP(S) endpoint, so if you run the URL, it will execute the cloud function we just deployed and return 'hello'.
glcoud function deploy options
For all the other options for deploy cloud function, you can reference the official document at here.gcloud functions deploy
Conclusion
This blog covered how to setup fireStore, and how to use Node.js to interact with fireStore with different operations.