google cloud spanner quickstart with a docker emulator instance

587 Views Asked by At

Was trying to run the nodejs quickstart for google cloud spanner. I started of the emulator instance via running below command on my development server:

docker run -p 9010:9010 -p 9020:9020 gcr.io/cloud-spanner-emulator/emulator

On the development server I could also create instances as follows:

# configuration first

gcloud config configurations create emulator
gcloud config set auth/disable_credentials true
gcloud config set project my-project
gcloud config set api_endpoint_overrides/spanner http://localhost:9020/

# creating instance

gcloud spanner instances create test-instance \
   --config=emulator-config --description="Test Instance" --nodes=1

I could successfully create instance.

Now I am trying to run the quick start samples from a different machine on the same network. I made the following changes in the schema.js file (line number 30).

  const spanner = new Spanner({
    projectId: projectId,
    apiEndpoint: 'http://dev-server-ip',
    port: 9020
  });

And I run the program as follows using nodejs:

node schema.js createDatabase test-instance example-db my-project

I got the following error:

schema.js createDatabase <instanceName> <databaseName> <projectId>

Creates an example database with two tables in a Cloud Spanner instance.

Options:
  --version  Show version number
 [boolean]
  --help     Show help
 [boolean]

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-
started for more information.
    at GoogleAuth.getApplicationDefaultAsync (D:\work\gcloud-connectors\nodejs-spanner\samples\node_modules\go
ogle-auth-library\build\src\auth\googleauth.js:183:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async GoogleAuth.getClient (D:\work\gcloud-connectors\nodejs-spanner\samples\node_modules\google-auth-l
ibrary\build\src\auth\googleauth.js:565:17)
    at async GrpcClient._getCredentials (D:\work\gcloud-connectors\nodejs-spanner\samples\node_modules\google-
gax\build\src\grpc.js:145:24)
    at async GrpcClient.createStub (D:\work\gcloud-connectors\nodejs-spanner\samples\node_modules\google-gax\b
uild\src\grpc.js:308:23)

EDIT

Issue resolved. You need to set the environment variable.

export SPANNER_EMULATOR_HOST=dev-server-ip:9010

Note the port is the gRPC port. 9010. The code change for the Spanner constructor is also not necessary.

0

There are 0 best solutions below