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

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.

    Enable the APIs

  8. Install and initialize the gcloud CLI.
  9. Update and install gcloud components.
    gcloud components update
  10. Prepare your development environment.

    Go to the Node.js setup guide

Create your function

  1. 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
    
  2. Create an index.js file in the helloworld directory with the following contents:

    const functions = require('@google-cloud/functions-framework');
    
    // Register an HTTP function with the Functions Framework that will be executed
    // when you make an HTTP request to the deployed function's endpoint.
    functions.http('helloGET', (req, res) => {
      res.send('Hello World!');
    });

    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 the npm command:

    1. Run the npm init command from the helloworld directory. Press Enter to accept the default answer to its questions.

      npm init
      
    2. 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:

{
  "name": "nodejs-docs-samples-functions-hello-world-get",
  "version": "0.0.1",
  "private": true,
  "license": "Apache-2.0",
  "author": "Google Inc.",
  "repository": {
    "type": "git",
    "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
  },
  "engines": {
    "node": ">=16.0.0"
  },
  "scripts": {
    "test": "c8 mocha -p -j 2 test/*.test.js --timeout=6000 --exit"
  },
  "dependencies": {
    "@google-cloud/functions-framework": "^3.1.0"
  },
  "devDependencies": {
    "c8": "^10.0.0",
    "gaxios": "^6.0.0",
    "mocha": "^10.0.0",
    "wait-port": "^1.0.4"
  }
}

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.

  1. Run the following command from the helloworld directory to install the Functions Framework on your local machine:

    npm install @google-cloud/functions-framework
    
  2. Run this command from the helloworld directory to run your function locally:

    npx @google-cloud/functions-framework --target=helloGET
    
  3. Test your function by visiting http://localhost:8080 in a browser or by running curl 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

  1. After the function deploys, note the uri property from the output of the gcloud 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).

  2. 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.