Use Cloud Functions
Objectives
Write, deploy, and trigger an HTTP Cloud Function that accesses Cloud 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 nodejs18 --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred Node.js version:
nodejs18
(recommended)nodejs20
(preview)nodejs16
nodejs14
nodejs12
nodejs10
Python
gcloud functions deploy bigtable_read_data \ --runtime python311 --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred Python version:
python311
(recommended)python310
python39
python38
python37
Go
gcloud functions deploy BigtableRead \ --runtime go120 --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred Go version:
go120
(recommended)go119
go118
go116
go113
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
- Review additional Bigtable code samples.
- Explore Bigtable integrations.
- Learn how to use Bigtable with Memcached.