Objectives
Write, deploy, and trigger an HTTP Cloud Run function that accesses Spanner.
Costs
This document uses Spanner and Cloud Run functions, which are billable components of Google Cloud.
For information on the cost of using Spanner, see Spanner pricing.
For information on the cost of using Cloud Run functions, including free invocations, see Cloud Run functions pricing.
Before you begin
This document assumes you have a Spanner instance named
test-instance
and a database namedexample-db
that uses the music application schema. For instructions on creating an instance and database with the music application schema, see Quickstart using the console or the tutorials for Getting Started in Go, Java, Node.js, or Python.Enable the Cloud Run functions and Cloud Build APIs.
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.
Java
git clone https://github.com/GoogleCloudPlatform/java-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 for accessing Spanner:
Node.js
cd nodejs-docs-samples/functions/spanner/
Python
cd python-docs-samples/functions/spanner/
Go
cd golang-samples/functions/spanner/
Java
cd java-docs-samples/functions/spanner/
Take a look at the sample code:
Node.js
Python
Go
Java
The function sends a SQL query to fetch all
Albums
data from your database. The function executes 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 spanner
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 spanner_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 HelloSpanner \ --runtime go121 --trigger-http
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-spanner-function \ --entry-point functions.HelloSpanner \ --runtime java17 \ --memory 512MB --trigger-http
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
Function deployment might take up to two minutes.
Note the url
value returned when your function finishes deploying. You will
use it when you trigger the function.
You can view your deployed functions on the Cloud Run 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"
Python
curl "https://REGION-PROJECT_ID.cloudfunctions.net/spanner_read_data"
Go
curl "https://REGION-PROJECT_ID.cloudfunctions.net/HelloSpanner"
Java
curl "https://REGION-PROJECT_ID.cloudfunctions.net/java-spanner-function"
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
SQL query, assuming you worked through a Getting Started
tutorial and populated the database:
SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold Your Peace
SingerId: 1, AlbumId: 2, AlbumTitle: Go, Go, Go
SingerId: 2, AlbumId: 1, AlbumTitle: Green
SingerId: 2, AlbumId: 3, AlbumTitle: Terrified
SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk
You can also visit the function's URL in your browser to see the result of your SQL query.
Cleanup
To avoid incurring additional charges to your Google Cloud account for the Spanner and Cloud Run functions resources used in this document, do the following:
Delete the instance:
gcloud spanner instances delete test-instance
Delete the function that you deployed:
Node.js
gcloud functions delete get
Python
gcloud functions delete spanner_read_data
Go
gcloud functions delete HelloSpanner
Java
gcloud functions delete java-spanner-function