Objectives
Write, deploy, and trigger an HTTP Cloud Function that accesses Bigtable.
Costs
This topic uses Bigtable and Cloud Functions, which are billable components of Google Cloud.
For information on the cost of using Bigtable, see Bigtable pricing.
For information on the cost of using Cloud Functions, including free invocations, see Cloud Functions pricing.
Before you begin
This topic assumes you have a Bigtable instance named
test-instance
and a table namedtest-table
. You can create these resources by following the steps on Creating a test table. Be sure to delete the resources when you are finished to avoid incurring unnecessary costs.Enable the Cloud Functions API.
Install and initialize the gcloud CLI.
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
Prepare your development environment:
Prepare the application
Clone the sample app repository to your local machine:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Functions sample code for accessing Bigtable:
Node.js
cd nodejs-docs-samples/functions/bigtable/
Python
cd python-docs-samples/functions/bigtable/
Go
cd golang-samples/functions/bigtable/
Take a look at the sample code:
Node.js
Python
Go
The function sends a read request to the table to fetch all
stats_summary
data for rows with a row key prefix ofphone
. The function is executed when you make an HTTP request to the function's endpoint.
Deploy the function
To deploy the function with an HTTP trigger, run the following
command in the bigtable
directory:
Node.js
gcloud functions deploy get \ --runtime nodejs20 --trigger-http
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy bigtable_read_data \ --runtime python312 --trigger-http
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy BigtableRead \ --runtime go121 --trigger-http
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Function deployment may take up to two minutes.
When your function finishes deploying, it returns the url
value. You will
use that value when you trigger the function.
You can view your deployed functions on the Cloud Functions page in the Google Cloud console. You can also create and edit functions on that page, and get details and diagnostics for your functions.
Trigger the function
Make an HTTP request to your function:
Node.js
curl "https://REGION-PROJECT_ID.cloudfunctions.net/get" -H "instance_id: test-instance" -H "table_id: test-table"
Python
curl "https://REGION-PROJECT_ID.cloudfunctions.net/bigtable_read_data" -H "instance_id: test-instance" -H "table_id: test-table"
Go
curl "https://REGION-PROJECT_ID.cloudfunctions.net/BigtableRead" -H "instance_id: test-instance" -H "table_id: test-table"
where REGION
and PROJECT_ID
match the values that are visible in your terminal when your function
finishes deploying. You should see output that shows the results of the read
request.
You can also visit the function's URL in your browser to see the result of your read request.
Clean up
To avoid incurring additional charges to your Google Cloud account for the Bigtable and Cloud Functions resources used in this topic:
Delete the instance:
gcloud bigtable instances delete test-instance
Delete the function that you deployed:
Node.js
gcloud functions delete get
Python
gcloud functions delete bigtable_read_data
Go
gcloud functions delete BigtableRead
What's Next
- Learn more about writing Cloud Functions.
- Learn more about deploying Cloud Functions.
- Learn more about calling Cloud Functions.
- Learn more about monitoring Cloud Functions.