I want to connect a Cloud Run service to the same Firestore db used in my App (or viceversa). It looks like when selecting the Integration tab of the run Service, there is no way to select an existing Firestore db, hence it creates a new one. On the other side, in my Android App (but the question can be extended to other apps as well) I don't know how to use a Firestore db other than the default one.
Solved! Go to Solution.
Connecting a Cloud Run service to an existing Firestore database, or configuring an app to use a non-default Firestore instance involves specific considerations and steps. Here's a refined guide:
1. Using Firestore in Cloud Run
Environment Variables: Setting environment variables such as GOOGLE_CLOUD_PROJECT
is a highly recommended best practice. This clarifies the project context, particularly in multi-project environments, when using local emulators for development, and helps prevent accidental service interactions with the wrong project.
Client Initialization:
// Node.js Example
const {Firestore} = require('@google-cloud/firestore');
const firestore = new Firestore({ projectId: 'your-firestore-project-id' });
2. Using a Non-Default Firestore Database in an App
FirebaseOptions
to explicitly specify the Firestore instance by setting the project ID and other necessary credentials. This is required when your Firebase project uses multiple Firestore instances, or when you need to target a specific instance other than the default one provided by the google-services.json
file.3. Ensuring Permissions and APIs are Enabled
roles/datastore.user
(Firestore uses the same permissions as Cloud Datastore), which allow the necessary read and write access to Firestore.4. Avoiding Automatic Creation of Resources
Additional Best Practices
Connecting a Cloud Run service to an existing Firestore database, or configuring an app to use a non-default Firestore instance involves specific considerations and steps. Here's a refined guide:
1. Using Firestore in Cloud Run
Environment Variables: Setting environment variables such as GOOGLE_CLOUD_PROJECT
is a highly recommended best practice. This clarifies the project context, particularly in multi-project environments, when using local emulators for development, and helps prevent accidental service interactions with the wrong project.
Client Initialization:
// Node.js Example
const {Firestore} = require('@google-cloud/firestore');
const firestore = new Firestore({ projectId: 'your-firestore-project-id' });
2. Using a Non-Default Firestore Database in an App
FirebaseOptions
to explicitly specify the Firestore instance by setting the project ID and other necessary credentials. This is required when your Firebase project uses multiple Firestore instances, or when you need to target a specific instance other than the default one provided by the google-services.json
file.3. Ensuring Permissions and APIs are Enabled
roles/datastore.user
(Firestore uses the same permissions as Cloud Datastore), which allow the necessary read and write access to Firestore.4. Avoiding Automatic Creation of Resources
Additional Best Practices
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |