Cloud functions dev and production environment

2,488 views
Skip to first unread message

Francois Schaus

unread,
Jun 27, 2018, 7:18:25 PM6/27/18
to Firebase Google Group
Hello,

As my app entered production a few weeks ago, I would like to have a proper separation between my production and dev environments. I was able to create two firestore databases (as two separate projects) and to link them to my two deployment targets (I followed the steps in a firecast tutorial I believe). 

Where I am struggling is with the creation of two cloud functions environments and to have them correctly point to my two firestore DBs.  I was able to use the CLI to create to environments (with the use command) and the triggers work properly (e.g., when a new doc gets created) but then it can't properly read/ write data on my db (e.g., the get() function returns nil). I am also getting the following error code: "Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions".


Could you point me to any documentation/ tutorials/ posts on how to properly set-up a separate cloud function environment and how to tie it with a new Firestore database? Also let me know if it's simply not supported (which would be a real barrier to adopting firebase/ firestore/ cloud functions in production)...

Thanks for the help!
Francois

Michael Bleigh

unread,
Jun 27, 2018, 8:04:41 PM6/27/18
to Firebase Google Group
I'm not sure I 100% follow your need here, but let me provide some general guidance:
  1. Each "environment" should be its own project. It seems like you're already doing that.
  2. Each project should contain all of the resources needed for itself. Keep your dev functions and dev database in the same project.
  3. Within a function, you should use application default credentials to make requests with the Firebase Admin SDKs.
  4. You can use Firebase CLI project aliases to switch between projects for deployment.
  5. You will need to upgrade all of your projects to a paid plan for Cloud Functions to make external network requests. For your dev project, I recommend the Blaze plan as you will likely pay less than Flame.
I hope that's somewhat helpful, and if there are specific problems let me know.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/73eec0f9-cb43-43b8-839d-632e8a6e1d1f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Francois Schaus

unread,
Jun 28, 2018, 7:16:56 PM6/28/18
to Firebase Google Group

Thanks a lot for taking the time to answer Michael! The step-by-step list below is super helpful and I would just have a few follow-up questions:
  1. Each "environment" should be its own project. It seems like you're already doing that. --> Yes, that's done.
  2. Each project should contain all of the resources needed for itself. Keep your dev functions and dev database in the same project. --> Yes. The only resource that's shared is my code base (obviously), but I deploy it to my separate instances using aliases (point 4 below).
  3. Within a function, you should use application default credentials to make requests with the Firebase Admin SDKs. --> That's maybe where I think I make a mistake. Since I have a single JS source-code that I deploy to my two projects, I might be missing a flag to point my cloud function to the right set credentials depending on which project is running (my dev or my prod project) - see my code below.
  4. You can use Firebase CLI project aliases to switch between projects for deployment. --> Yup I do that.
  5. You will need to upgrade all of your projects to a paid plan for Cloud Functions to make external network requests. For your dev project, I recommend the Blaze plan as you will likely pay less than Flame. --> Yes, but my app doesn't do any "external" network requests (depending on how you define it) - it monitors my DB (internal) and send triggers to FCM (internal?). It never communicates directly with my clients nor does it call other external servers...
To load both my credentials in my Index.js file, I use the code below to initialize both environments. What I realize is that I might need a "flag" to only initialize one of the two environment. Is there a simple way I could do that by e.g., looking with firebase project my functions are running in (myApp vs. myApp-dev) or which alias was used to deploy them in the CLI?

Thanks again for your help, it would really be awesome if I could finally solve this issue once and for all (as I am dreading the day I'll do a mistake and accidentally prune all my data on my prod environment while doing some on-going dev/ debugging).

Best,
Francois

const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
// admin.initializeApp();

// Both of these files are deployed to my dev & prod functions. They contain the plist info of my two projects
var serviceAccount = require("./adminKey.json");
var serviceAccountDev = require("./adminKey-dev.json");

var secondEnvironment = admin.initializeApp({
 credential: admin.credential.cert(serviceAccountDev),
}, 'dev instance');

admin.initializeApp({
 credential: admin.credential.cert(serviceAccount),
});

var db = admin.firestore();


Reply all
Reply to author
Forward
0 new messages