## Objectives

Write, deploy, and trigger an
[HTTP Cloud Run function](https://docs.cloud.google.com/functions/1stgendocs/writing/write-http-functions.md.txt)
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](https://cloud.google.com/spanner/pricing).

- For information on the cost of using Cloud Run functions, including free
  invocations, see [Cloud Run functions pricing](https://cloud.google.com/functions/pricing).

## Before you begin

1. This document assumes you have a Spanner instance named
   `test-instance` and a database named `example-db` that uses the music
   application
   [schema](https://docs.cloud.google.com/spanner/docs/schema-and-data-model.md.txt#creating-interleaved-tables).
   For instructions on creating an instance and database with the music application
   schema, see [Quickstart using the console](https://docs.cloud.google.com/spanner/docs/create-query-database-console.md.txt) or
   the tutorials for Getting Started in [Go](https://docs.cloud.google.com/spanner/docs/getting-started/go.md.txt),
   [Java](https://docs.cloud.google.com/spanner/docs/getting-started/java.md.txt),
   [Node.js](https://docs.cloud.google.com/spanner/docs/getting-started/nodejs.md.txt), or
   [Python](https://docs.cloud.google.com/spanner/docs/getting-started/python.md.txt).

   > [!NOTE]
   > **Note:** After this step, you should have two tables, [`Singers`](https://docs.cloud.google.com/spanner/docs/schema-and-data-model.md.txt#creating_a_table) and [`Albums`](https://docs.cloud.google.com/spanner/docs/schema-and-data-model.md.txt#creating_multiple_tables), in your database with the sample data specified in the music application schema.

2. Enable the Cloud Run functions and Cloud Build APIs.

   [Enable the APIs](https://console.cloud.google.com/apis/enableflow?apiid=cloudfunctions.googleapis.com,cloudbuild.googleapis.com)
3. [Install and initialize the gcloud CLI](https://docs.cloud.google.com/sdk/docs.md.txt).

   If you already have the gcloud CLI installed, update it by running the
   following command:

       gcloud components update

4. Prepare your development environment:


   ### Node.js


   [Go to the Node.js setup guide](https://docs.cloud.google.com/nodejs/docs/setup.md.txt)

   ### Python


   [Go to the Python setup guide](https://docs.cloud.google.com/python/docs/setup.md.txt)

   ### Go


   [Go to the Go setup guide](https://docs.cloud.google.com/go/docs/setup.md.txt)

   ### Java


   [Go to the Java setup guide](https://docs.cloud.google.com/java/docs/setup.md.txt)

   <br />

## Prepare the application

1. Clone the sample app repository to your local machine:

   ### Node.js

   ```sh
   git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/archive/main.zip)
   as a zip file and extract it.

   ### Python

   ```sh
   git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/python-docs-samples/archive/main.zip)
   as a zip file and extract it.

   ### Go

   ```sh
   git clone https://github.com/GoogleCloudPlatform/golang-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/golang-samples/archive/main.zip)
   as a zip file and extract it.

   ### Java

   ```sh
   git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/java-docs-samples/archive/main.zip)
   as a zip file and extract it.
2. Change to the directory that contains the Cloud Run functions sample
   code for accessing Spanner:

   ### Node.js

   ```sh
   cd nodejs-docs-samples/functions/spanner/
   ```

   ### Python

   ```sh
   cd python-docs-samples/functions/spanner/
   ```

   ### Go

   ```sh
   cd golang-samples/functions/spanner/
   ```

   ### Java

   ```sh
   cd java-docs-samples/functions/spanner/
   ```
3. Take a look at the sample code:


   ### Node.js

   ```javascript
   // Imports the Google Cloud client library
   const {Spanner} = require('@google-cloud/spanner');

   // Imports the functions framework to register your HTTP function
   const functions = require('@google-cloud/functions-framework');

   // Instantiates a client
   const spanner = new Spanner();

   // Your Cloud Spanner instance ID
   const instanceId = 'test-instance';

   // Your Cloud Spanner database ID
   const databaseId = 'example-db';

   /**
    * HTTP Cloud Function.
    *
    * @param {Object} req Cloud Function request context.
    * @param {Object} res Cloud Function response context.
    */
   functions.http('spannerQuickstart', async (req, res) => {
     // Gets a reference to a Cloud Spanner instance and database
     const instance = spanner.instance(instanceId);
     const database = instance.database(databaseId);

     // The query to execute
     const query = {
       sql: 'SELECT * FROM Albums',
     };

     // Execute the query
     try {
       const results = await database.run(query);
       const rows = results[0].map(row => row.toJSON());
       rows.forEach(row => {
         res.write(
           `SingerId: ${row.SingerId}, ` +
             `AlbumId: ${row.AlbumId}, ` +
             `AlbumTitle: ${row.AlbumTitle}\n`
         );
       });
       res.status(200).end();
     } catch (err) {
       res.status(500).send(`Error querying Spanner: ${err}`);
     }
   });
   ```

   ### Python

   ```python
   import functions_framework
   from google.cloud import spanner

   instance_id = "test-instance"
   database_id = "example-db"

   client = spanner.Client()
   instance = client.instance(instance_id)
   database = instance.database(database_id)


   @functions_framework.http
   def spanner_read_data(request):
       query = "SELECT * FROM Albums"

       outputs = []
       with database.snapshot() as snapshot:
           results = snapshot.execute_sql(query)

           for row in results:
               output = "SingerId: {}, AlbumId: {}, AlbumTitle: {}".format(*row)
               outputs.append(output)

       return "\n".join(outputs)

   ```

   ### Go

   ```go

   // Package spanner contains an example of using Spanner from a Cloud Function.
   package spanner

   import (
   	"context"
   	"fmt"
   	"log"
   	"net/http"
   	"sync"

   	"cloud.google.com/go/spanner"
   	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
   	"google.golang.org/api/iterator"
   )

   // client is a global Spanner client, to avoid initializing a new client for
   // every request.
   var client *spanner.Client
   var clientOnce sync.Once

   // db is the name of the database to query.
   var db = "projects/my-project/instances/my-instance/databases/example-db"

   func init() {
   	functions.HTTP("HelloSpanner", HelloSpanner)
   }

   // HelloSpanner is an example of querying Spanner from a Cloud Function.
   func HelloSpanner(w http.ResponseWriter, r *http.Request) {
   	clientOnce.Do(func() {
   		// Declare a separate err variable to avoid shadowing client.
   		var err error
   		client, err = spanner.NewClient(context.Background(), db)
   		if err != nil {
   			http.Error(w, "Error initializing database", http.StatusInternalServerError)
   			log.Printf("spanner.NewClient: %v", err)
   			return
   		}
   	})

   	fmt.Fprintln(w, "Albums:")
   	stmt := spanner.Statement{SQL: `SELECT SingerId, AlbumId, AlbumTitle FROM Albums`}
   	iter := client.Single().Query(r.Context(), stmt)
   	defer iter.Stop()
   	for {
   		row, err := iter.Next()
   		if err == iterator.Done {
   			return
   		}
   		if err != nil {
   			http.Error(w, "Error querying database", http.StatusInternalServerError)
   			log.Printf("iter.Next: %v", err)
   			return
   		}
   		var singerID, albumID int64
   		var albumTitle string
   		if err := row.Columns(&singerID, &albumID, &albumTitle); err != nil {
   			http.Error(w, "Error parsing database response", http.StatusInternalServerError)
   			log.Printf("row.Columns: %v", err)
   			return
   		}
   		fmt.Fprintf(w, "%d %d %s\n", singerID, albumID, albumTitle)
   	}
   }
   ```

   ### Java

   ```java
   import com.google.api.client.http.HttpStatusCodes;
   import com.google.cloud.functions.HttpFunction;
   import com.google.cloud.functions.HttpRequest;
   import com.google.cloud.functions.HttpResponse;
   import com.google.cloud.spanner.DatabaseClient;
   import com.google.cloud.spanner.DatabaseId;
   import com.google.cloud.spanner.LazySpannerInitializer;
   import com.google.cloud.spanner.ResultSet;
   import com.google.cloud.spanner.SpannerException;
   import com.google.cloud.spanner.SpannerOptions;
   import com.google.cloud.spanner.Statement;
   import com.google.common.annotations.VisibleForTesting;
   import com.google.common.base.MoreObjects;
   import java.io.PrintWriter;
   import java.util.logging.Level;
   import java.util.logging.Logger;

   // HelloSpanner is an example of querying Spanner from a Cloud Function.
   public class HelloSpanner implements HttpFunction {
     private static final Logger logger = Logger.getLogger(HelloSpanner.class.getName());

     // TODO<developer>: Set these environment variables.
     private static final String SPANNER_INSTANCE_ID =
         MoreObjects.firstNonNull(System.getenv("SPANNER_INSTANCE"), "my-instance");
     private static final String SPANNER_DATABASE_ID =
         MoreObjects.firstNonNull(System.getenv("SPANNER_DATABASE"), "example-db");

     private static final DatabaseId databaseId =
         DatabaseId.of(
             SpannerOptions.getDefaultProjectId(),
             SPANNER_INSTANCE_ID,
             SPANNER_DATABASE_ID);

     // The LazySpannerInitializer instance is shared across all instances of the HelloSpanner class.
     // It will create a Spanner instance the first time one is requested, and continue to return that
     // instance for all subsequent requests.
     private static final LazySpannerInitializer SPANNER_INITIALIZER = new LazySpannerInitializer();

     @VisibleForTesting
     DatabaseClient getClient() throws Throwable {
       return SPANNER_INITIALIZER.get().getDatabaseClient(databaseId);
     }

     @Override
     public void service(HttpRequest request, HttpResponse response) throws Exception {
       var writer = new PrintWriter(response.getWriter());
       try {
         DatabaseClient client = getClient();
         try (ResultSet rs =
             client
                 .singleUse()
                 .executeQuery(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"))) {
           writer.printf("Albums:%n");
           while (rs.next()) {
             writer.printf(
                 "%d %d %s%n",
                 rs.getLong("SingerId"), rs.getLong("AlbumId"), rs.getString("AlbumTitle"));
           }
         } catch (SpannerException e) {
           writer.printf("Error querying database: %s%n", e.getMessage());
           response.setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, e.getMessage());
         }
       } catch (Throwable t) {
         logger.log(Level.SEVERE, "Spanner example failed", t);
         writer.printf("Error setting up Spanner: %s%n", t.getMessage());
         response.setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, t.getMessage());
       }
     }
   }
   ```

   <br />

   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

```sh
gcloud functions deploy get \
--runtime nodejs22 --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Node.js version](https://docs.cloud.google.com/static/functions/docs/runtime-support#node.js) to run
your function.


### Python

```sh
gcloud functions deploy spanner_read_data \
--runtime python312 --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Python version](https://docs.cloud.google.com/functions/docs/runtime-support.md.txt#python) to run
your function.


### Go

```sh
gcloud functions deploy HelloSpanner \
--runtime go121 --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Go version](https://docs.cloud.google.com/functions/docs/runtime-support.md.txt#go) to run
your function.


### Java

```sh
gcloud functions deploy java-spanner-function \
--entry-point functions.HelloSpanner \
--runtime java17 \
--memory 512MB --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Java version](https://docs.cloud.google.com/functions/docs/runtime-support.md.txt#java) 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](https://console.cloud.google.com/project/_/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

```sh
curl "https://REGION-PROJECT_ID.cloudfunctions.net/get" 
```

### Python

```sh
curl "https://REGION-PROJECT_ID.cloudfunctions.net/spanner_read_data" 
```

### Go

```sh
curl "https://REGION-PROJECT_ID.cloudfunctions.net/HelloSpanner" 
```

### Java

```sh
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](https://docs.cloud.google.com/spanner/docs/tutorials) 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:

1. Delete the instance:

       gcloud spanner instances delete test-instance

2. Delete the function that you deployed:


   ### Node.js

   ```sh
   gcloud functions delete get 
   ```

   ### Python

   ```sh
   gcloud functions delete spanner_read_data 
   ```

   ### Go

   ```sh
   gcloud functions delete HelloSpanner 
   ```

   ### Java

   ```sh
   gcloud functions delete java-spanner-function 
   ```

   <br />

## What's next

- [Learn more about writing Cloud Run functions](https://docs.cloud.google.com/functions/1stgendocs/writing.md.txt).
- [Learn more about deploying Cloud Run functions](https://docs.cloud.google.com/functions/1stgendocs/deploy.md.txt).
- [Learn more about calling Cloud Run functions](https://docs.cloud.google.com/functions/1stgendocs/calling.md.txt).
- [Learn more about monitoring Cloud Run functions](https://docs.cloud.google.com/functions/1stgendocs/monitoring.md.txt).