Create and deploy an HTTP Cloud Run function by using Node.js (1st gen)
This guide takes you through the process of writing a Cloud Run function using the Node.js runtime. There are two types of Cloud Run functions:
- An HTTP function, which you invoke from standard HTTP requests.
- An event-driven function, which you use to handle events from your Cloud infrastructure, such as messages on a Pub/Sub topic, or changes in a Cloud Storage bucket.
The sample shows how to create a simple HTTP function.
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 and Cloud Build 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 and Cloud Build APIs.
- Install and initialize the gcloud CLI.
- Update and install
gcloud
components:gcloud components update
- Prepare your development environment.
Create a 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 takes a name supplied in the HTTP request and returns a greeting, or "Hello World!" when no name is supplied.
Specify dependencies
Dependencies in Node.js are stored in a file called
package.json
.
You can create this file manually or with the
npm command.
To create your
package.json
dependency file by usingnpm
, run these commands:npm init npm install c8 gaxios mocha sinon supertest wait-port --save-dev npm install @google-cloud/functions-framework escape-html
If you prefer to build your
package.json
file manually, create apackage.json
file in thehelloworld
directory with the following contents:
Deploy the function
To deploy the function with an HTTP trigger, run the following
command in the helloworld
directory:
gcloud functions deploy helloHttp --no-gen2 --runtime nodejs22 --trigger-http --allow-unauthenticated
The --allow-unauthenticated
flag lets you reach the function
without authentication.
To require
authentication, omit the
flag.
Test the function
When the function finishes deploying, take note of the
httpsTrigger.url
property or find it using the following command:gcloud functions describe helloHttp
It should look like this:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloHttp
Visit this URL in your browser. You should see a "Hello World!" message.
Try passing a name in the HTTP request, for example by using the following URL:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloHttp?name=NAME
You should see the message "Hello
NAME
!"
View logs
Logs for Cloud Run functions are viewable using the Google Cloud CLI, and in the Cloud Logging UI.
Use the command-line tool
To view logs for your function with the gcloud CLI, use the
logs read
command, followed by
the name of the function:
gcloud functions logs read helloHttp
The output should resemble the following:
LEVEL NAME EXECUTION_ID TIME_UTC LOG D helloHttp rvb9j0axfclb 2019-09-18 22:06:25.983 Function execution started D helloHttp rvb9j0axfclb 2019-09-18 22:06:26.001 Function execution took 19 ms, finished with status code: 200
Use the Logging dashboard
You can also view logs for Cloud Run functions from the Google Cloud console.