Create and deploy an HTTP Cloud Run function with Node.js
This document takes you through the process of creating a simple Cloud Run functions HTTP function. This is one of the two types of Cloud Run functions:
- An HTTP function, which you invoke from standard HTTP requests.
- An event-driven function, which is triggered by events in your Cloud infrastructure, such as messages on a Pub/Sub topic or changes in a Cloud Storage bucket.
For more detail, read writing HTTP functions and writing event-driven functions.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.
- Install and initialize the gcloud CLI.
- Update and install
gcloud
components.gcloud components update
-
Prepare your development environment.
Create your function
Create a directory on your local system for the function code:
Linux or Mac OS X
mkdir ~/helloworld cd ~/helloworld
Windows
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworld
Create an
index.js
file in thehelloworld
directory with the following contents:This example function returns a cheery "Hello World!" to all requests.
Specify dependencies
Dependencies in Node.js are managed with npm
and expressed in a metadata file called
package.json
. You can create this
file manually or with the npm
command.
To create your
package.json
file with thenpm
command:Run the
npm init
command from the helloworld directory. PressEnter
to accept the default answer to its questions.npm init
Edit the
package.json
file to add a functions-framework dependency:"dependencies": { "@google-cloud/functions-framework": "^3.1.0" }
If you prefer to create your
package.json
file manually, copy the following contents into it:
Many Node.js client libraries are available for use with Google Cloud products and can be installed as dependencies.
Build and test your function locally
To test your function locally before deploying it, you must install the Functions Framework locally, then run the function.
Run the following command from the
helloworld
directory to install the Functions Framework on your local machine:npm install @google-cloud/functions-framework
Run this command from the
helloworld
directory to run your function locally:npx @google-cloud/functions-framework --target=helloGET
Test your function by visiting
http://localhost:8080
in a browser or by runningcurl localhost:8080
from another window.See Sending requests to local functions for more detail.
The function returns the message "Hello World!"
Deploy your function
To deploy your function, run the
gcloud functions deploy
command in the helloworld
directory:
gcloud functions deploy hello-node-function \
--gen2 \
--runtime=nodejs20 \
--region=REGION \
--source=. \
--entry-point=helloGET \
--trigger-http \
--allow-unauthenticated
Replace REGION with the name of the Google Cloud region where you want to deploy your function
(for example, us-west1
).
This deploys your sample function with the nodejs20 runtime in your chosen region.
The optional --allow-unauthenticated
flag lets you reach your function
without authentication.
Test your deployed function
After the function deploys, note the
uri
property from the output of thegcloud functions deploy
command, or retrieve it with the following command:gcloud functions describe hello-node-function \ --region=REGION
Replace REGION with the name of the Google Cloud region where you deployed your function (for example,
us-west1
).Visit this URL in your browser or with the following
curl
command:curl FUNCTION_URL
Replace FUNCTION_URL with the
uri
property you've just retrieved.The function returns a "Hello World!" message.
View your function's logs
View logs with the command-line tool
You can review your function's logs with the Cloud Logging UI or using the Google Cloud CLI.
To view logs for your function with the gcloud CLI, use the
gcloud functions logs read
command:
gcloud functions logs read \
--gen2 \
--region=REGION \
--limit=10 \
hello-node-function
Replace REGION with the name of the Google Cloud region where you've deployed your function
(for example, us-west1
).
The output resembles the following:
LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:42:24.956
LOG:
LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:42:01.692
LOG:
LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:31:47.711
LOG: Default STARTUP TCP probe succeeded after 1 attempt for container "hello--node--function-1" on port 8080.
LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:31:46.542
LOG:
LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:31:27.390
LOG: Default STARTUP TCP probe succeeded after 1 attempt for container "hello--node--function-1" on port 8080.
View logs with the logging dashboard
To view the logs for your function with the logging dashboard, open the Cloud Run functions Overview page and click the name of your function from the list, then click the Logs tab.