How to Deploy Cloud Functions with Node.js
Table of Contents
- Introduction
- About Cloud Functions
- GCP Account
- Create Node.js Function for Cloud Functions
- How to Test Cloud Function Locally
- How to Deploy Cloud Function to the Cloud
- gcloud Function Deploy Options
- Conclusion
Introduction
Google's Cloud Functions let you run code and deploy to the cloud with no servers or containers to maintain. In this blog, we'll write our first cloud function and deploy it to the cloud.
About Cloud Functions
Google's Cloud Functions let you run code and deploy to GCP with no servers or containers you need to maintain. This makes it ideal for lightweight, event-driven workloads.
GCP Account
First, you'll need a GCP account. Once you have one, you should be able to proceed with the steps below.
Create Node.js Function for Cloud Functions
This command will create a package.json file.
npm init -y
Then run this command to install the package @google-cloud/functions-framework for testing cloud functions locally.
npm install --save-dev @google-cloud/functions-framework
Create our first Node.js function index.js. Inside the index.js file, we'll add the following code, which simply returns the message "hello".
exports.main = (req, res) => {
res
.status(200)
.send('hello');
}
How to Test Cloud Function Locally
Add the following section to the package.json file's script section and run npm run start.
You should see URL: http://localhost:8080, which will host the code from index.js. Since we specified the method as main, it will run our main function, and you should see "hello" in the browser.
"scripts": {
"start": "npx functions-framework --target=main [--signature-type=http]"
},
How to Deploy Cloud Function to the Cloud
After testing locally, you're ready to deploy the function to the cloud. This section will show how to run the gcloud command from the local environment and deploy to the cloud.
Config gcloud
To run gcloud commands locally, you'll need to configure it first. If you've already configured it before, you can run gcloud config list to verify that the account and project point to the correct ones, because you don't want to accidentally deploy to the wrong GCP account.
If you have not set it up before, you can run gcloud init to authenticate with your GCP account.
Once you've confirmed the configuration points to the correct environment, you can run the following command at the root folder containing your index.js.
gcloud functions deploy my-first-function --trigger-http --region=us-central1 --runtime=nodejs16 --gen2 --allow-unauthenticated --entry-point=main
These options are pretty straightforward: gcloud functions deploy is the command to deploy, my-first-function will be the name of the function shown on the Cloud Functions console, trigger-http means this will work as an HTTP(S) request, runtime specifies the version of the app during the build, and entry-point is the main function Cloud Functions will run.
Also, gen2 is the next version of Cloud Functions, providing longer request processing, support for more regions, and faster rollback.
At this point, if the above gcloud functions deploy command runs successfully, you should see a "done" message on your console along with other information, such as build, label, or the options you specified.
Also, the URL will provide an HTTP(S) endpoint. If you visit the URL, it will execute the cloud function we just deployed and return "hello".
gcloud Function Deploy Options
For all other options for deploying cloud functions, you can reference the official documentation here: gcloud functions deploy.
Conclusion
This blog covered how to create a Cloud Function with Node.js, test it locally using the functions framework, and deploy it to Google Cloud using the gcloud CLI. With Cloud Functions, you can quickly deploy serverless code without managing infrastructure.