Multiple firebase environments in different regions - firebase

I am building a web application and need a dev instance and a prod instance. In the past I've always made two separate firebase projects (project-name-dev and project-name-prod) and used aliases to deploy to each environment.
I now have a need to host the same project in two different regions (US data needs to live in US, Canada data needs to live in Canada). I still need a dev and prod instance. My current thought is to create two new projects totaling 4 separate firebase backends that all use the same frontend
project-name-us-dev
project-name-us-prod
project-name-ca-dev
project-name-ca-prod
The front end code is using Vue.js. (not sure if that's relevant or not)
Is there a simpler way of managing this project?
Thanks!

Since you tagged this question google-cloud-firestore, I'll assume that you are mostly referring to the location of the data in Firestore. I suggest reading this if you haven't already. Once you've chosen a region for Firestore, that project is locked into that location for the rest of its life, in addition to its default storage bucket as well as GAE. Since you can't have two Firestore instances in the same project, and you have to choose a location for Firestore in the project, it stands to reason that there isn't any easier way to manage your projects than what you're doing now.

Related

Same firebase account for different Play Console Account's apps?

I have 2 different Play Console accounts and each have a Stories app in them. Currently both of them are linked to same Firebase account (i.e. same database). I want to ask if this is ok or should I make a separate Firebase project for each app?
The short answer is, it depends.
If the two apps are identical or closely related (eg. an admin-only app that controls the flow of the client app), then yes, you'll want to configure both apps to use the same Firebase project so that they both have access to the same data.
Using the same Firebase project in multiple apps is pretty common, especially in the scenario outlined above as well as deploying to multiple platforms. Take, for example, an app that you have deployed on the web, iOS, as well as Android. These apps run on different platforms, but they are essentially the exact same app, with the exact same functionality. You don't want to be maintaining multiple projects containing duplicate data, so it's a good idea to connect different versions of the same app to the same Firebase project.
On the other hand, if these apps are not related at all (eg a quiz game and a social media app), then it doesn't make sense for them to be using the same Firebase project as the two apps are completely separate from one another. In this scenario, using the same project would lead to disorganized data, as well as making it potentially more difficult to secure and query as the apps have separate functions, and thus separate logic to control the flow of data.
It sounds like your use case falls into the first category. You have the same app on multiple Play Console accounts. You probably want the data to remain consistent between them, so you'd want to use a single Firebase project.

How to administrate this estructure on Firebase?

There are multiple companies using one application. So I want to have, or know if this is the best way, to create a project for each company, all this with the Firebase API, to create everything programmatically. But I think this is too difficult to do and get that structure. The truth is that the documentation about the Rest API to create projects is not very clear and I am getting lost. One of the things that I want to structure projects like this is to know how much use is made of each project, calculate requests and use of storage. But I don't know if there are other ways to do this.
It's not possible to create a new project for Firebase using the Firebase API, what is possible is to list projects, and create/list apps within an existing project using the Firebase Management API.
For monitoring usage, you can use Cloud Monitoring to assess the usage of each individual app, per service.

Use different data for production and develop firebase sites

I have a CI/DC pipeline with google cloud build triggers that deploy my code to different sites depending on which branch I push to. The develop site is a live test - the final check before I merge to master, which triggers a deploy of master to the production site.
Currently, both sites use the same firebase Firestore db, and any document changed on the develop site will also be changed on the production site.
What I want to avoid is creating another firebase project to push the develop code to with a different database, because that means I need a separate set of credentials and would copy the same functions over to the new project every time I change them. That's not maintainable and is a lot of work.
What I would like is some way for the develop site to only have access to part of the firestore database, and the production site to have access to another part.
How do people do this? Is it even possible? Is there a better way? One alternative I can think of is using authentication and creating separate accounts for testing with different access permissions, but this seems a work-around and not the ideal solution.
What you're trying to do sounds like a lot more hassle than using multiple projects, which is the documented and strongly preferred solution. Putting everything in one project is a huge anti-pattern in Firebase and Google Cloud, and it will cause you more problems in the long run, in addition to increasing the risk of catastrophic failure if you manage to misconfigure something in that one project.
It's perfectly maintainable to have multiple projects like this, if you apply some scripting to automate the work. This is very common, and I strongly suggest thinking through how this would work for you.
You CI/CD pipeline could definitely check out your updates from source control and deploy them to whatever other project environments you have set up. It's very common to manage different credentials and configurations for use in CI/CD.

How to run different cloud functions for seperate apps in the same Firebase project?

For example, if I wanted to use the functions.auth.user().onUserCreate() trigger, is there any way to make it trigger different functions for different apps that are all in the same Firebase project? Is the only way to fix this to create separate Firebase projects for each app? The issue with that is that I need them to access the same database.
It's not possible to know in a Cloud Functions trigger which app a user used when they created their account. Firebase Auth accounts don't have a sense of "ownership" with respect to multiple apps per project. All apps in a project share the same users with the same permissions.
What you can do instead is have each app write something unique in your database after the account was created, then use that to determine what that user should do later on. Either that, or use different projects if your apps don't actually need to share the same set of users.
The only possibility that I think that might help you is organizing your Cloud Functions in different files, which will be then loaded per project. So, for example, in your Project Alpha, it will run the Cloud Functions from the foo.js file, while in the Project Beta, it will run the functions from the bar.js and the same logic for other projects and Cloud Functions.
This way, even though all users are sharing the same database, it will trigger the function based in the file that you requiring in your application. You can check for tips in organizing your Cloud Functions in multiple files and calling them individually, in this official documentation here:
Organize Functions
I believe this way might work for you, so, I would recommend you to check the documentation and give it a try splitting the functions in multiple files and calling each specific files, on their specific applications only.
Let me know if the information helped you!

Access control on Firebase databases

We are having different environments for one of our solution (test, preprod, prod). We've created different Firebase databases as part of single project on Firebase console. We want to have access control implemented to these databases depending on users or groups e.g. prod Firebase database will be accessible to only few users where as other environment's (test and preprod) Firebase database will be accessible to most of the users.
Are we implementing it correctly? If yes, would it be possible to configure Firebase project to have this kind of setup.
This is not the recommended way to implement different development environments. The only recommended way to separate environments is to create different Firebase projects for each one. This ensures that they each have completely isolated data, users, security rules, and so on. Adding multiple databases to the same project isn't true isolation, and could lead to problems later on.

Resources