-
Make sure you have a Google Cloud project and billing is enabled.
-
Set your
PROJECT_ID
environment variable:export PROJECT_ID=<YOUR_PROJECT_ID>
-
Install the gcloud CLI.
-
Set gcloud project:
gcloud config set project $PROJECT_ID
-
Enable APIs:
gcloud services enable spanner.googleapis.com
-
Install python and set up a python virtual environment.
-
Make sure you have python version 3.11+ installed.
python -V
-
Set environment variables.
export INSTANCE=my-spanner-gsql-instance export DATABASE=assistantdemo export REGION=regional-us-central1 export INSTANCE_DESCRIPTION="My Spanner GSQL Instance"
-
Create a Cloud Spanner instance:
gcloud spanner instances create $INSTANCE \ --config=$REGION \ --nodes=1 \ --description=$INSTANCE_DESCRIPTION
-
Create a database within the Cloud Spanner instance:
gcloud spanner databases create $DATABASE --instance=$INSTANCE
-
Verify the database created with the
gcloud
tool:gcloud spanner databases execute-sql $DATABASE \ --instance=$INSTANCE \ --sql="SELECT 1"
-
Set environment variables.
export SA_NAME=spanner-service export SA_EMAIL=$SA_NAME@$PROJECT_ID.iam.gserviceaccount.com
-
Create a Service Account: Use the gcloud iam service-accounts create command to create a new service account.
gcloud iam service-accounts create $SA_NAME --description="Service account for Cloud Spanner" --display-name="Cloud Spanner Service Account"
-
Grant Required Permissions: Assign the necessary roles to the service account. For Cloud Spanner read and write access, you can grant the roles/spanner.databaseUser and roles/spanner.databaseAdmin roles. Use the gcloud projects add-iam-policy-binding command to grant these roles.
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SA_EMAIL" --role="roles/spanner.databaseUser" --condition=None gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SA_EMAIL" --role="roles/spanner.databaseAdmin" --condition=None
-
Generate a key file for the service account. This key file will be used for authentication when accessing GCP resources programmatically.
gcloud iam service-accounts keys create key.json --iam-account $SA_EMAIL
-
Use the generated key file (key.json) to authenticate your application when accessing Cloud Spanner.
-
Change into the retrieval service directory:
cd genai-databases-retrieval-app/retrieval_service
-
Install requirements:
pip install -r requirements.txt
-
Make a copy of
example-config.yml
and name itconfig.yml
.cp example-config.yml config.yml
-
Update
config.yml
with your database information.host: 0.0.0.0 datastore: # Example for Spanner kind: "spanner-gsql" project: <YOUR_PROJECT_ID> instance: my-spanner-gsql-instance database: assistantdemo service_account_key_file: <PATH_TO_SERVICE_ACCOUNT_KEY_FILE>
-
Populate data into database:
python run_database_init.py
Clean up after completing the demo.
-
Delete the Cloud Spanner instance:
gcloud spanner instances delete $INSTANCE