This tutorial demonstrates writing an HTTP Cloud Run function that submits a query to BigQuery.
Objectives
- Write an HTTP Cloud Run function that submits a query to BigQuery.
Costs
In this document, you use the following billable components of Google Cloud:
- Cloud Run functions
- Cloud Build
- Artifact Registry
For details, see Cloud Run functions pricing.
To generate a cost estimate based on your projected usage,
use the pricing calculator.
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, and Artifact Registry APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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, and Artifact Registry APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Prepare your development environment.
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
Preparing the application
Clone the sample app repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Run functions sample code:
cd nodejs-docs-samples/functions/v2/helloBigQuery
Take a look at the sample code. The sample submits a query for words that occur at least 400 times in the specified dataset, and returns the result.
Deploying the function
To deploy the function with an HTTP trigger, run the following command in the directory that contains the sample code:
gcloud functions deploy nodejs-bq-function \ --gen2 \ --runtime=nodejs22 \ --region=REGION \ --source=. \ --entry-point=helloBigQuery \ --trigger-http \ --allow-unauthenticated
You can use the following values for the
--runtime
flag to specify your preferred Node.js version:
nodejs18
(recommended)nodejs16
nodejs14
nodejs12
nodejs10
The --allow-unauthenticated
flag lets you reach the function
without authentication.
To require
authentication, omit the
flag.
Triggering the function
When the function finishes deploying, take note of the
uri
property or find it using the following command:gcloud functions describe nodejs-bq-function --gen2 --region=REGION --format="value(serviceConfig.uri)"
Visit this URI in your browser. You should see a list of the words that match the query criteria, and how many times each word appears in the target dataset.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Deleting the project
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Deleting the function
Deleting Cloud Run functions does not remove any resources stored in Cloud Storage.
To delete the function you created in this tutorial, run the following command:
gcloud functions delete nodejs-bq-function --gen2 --region REGION
You can also delete Cloud Run functions from the Google Cloud console.