structure firebase functions into multiple runtimes - firebase

I've been using firebase ecosystem for over 2 years but as it google lacks decent documentation i often come here to ask very basic things that we should learn right after "hello world".
When using firebase functions i try to modularize it to keep it readable and easy to maintain. the way i managed to do it was by having an "index" file and multiple subfiles which contains the logic for complex functions...
although it works very well, my index file is getting super long since i'm having more and more functions and it also needs to deal with some configuration for each of those specific functions...
i was messing around firebase dashboard https://console.cloud.google.com/functions/list? and i found that is possible to create a new function over this online form... when doing it the firebase backeend somehow create a new "runtime" for this function. I mean each function created by this form has its own "index.js" "package.json"
how can i do this without need to create every function from this form?
how can i simple code a new function ecosystem, deploy it using firebase cli and have this separeted structure for it?

All Cloud Functions are logically isolated from each other at all times at runtime. While they might share some common code at deployment, they don't share anything else.
The Firebase CLI requires that all your functions be defined in a single entrypoint, which is your index.js. That is just how it works. If you don't like that, you can deploy functions individually using gcloud, but you will not be able to use the firebase-functions module to declare and implement your function. gcloud uses different conventions.
If you want to continue to deploy with the Firebase CLI, you can add the new function to your index.js. It can be deployed separately from your other functions using the --only argument. For example, if your new function is called "fn":
firebase deploy --only functions:fn
This will deploy just fn and none of the other functions defined in your index. You can read about this and more options in the Firebase CLI documentation for deploying functions.
If you abosolutely do not want to have all your functions in a single index.js, you can split the definitions among multiple files, and require or import them into the main index.js. It's up to you how you want to organize your source file, using the facilities provided by nodejs and JavaScript.

Related

Firebase functions: Deploy only newly added functions

I happen to have over 100 cloud functions. I occasionally add new sets of functions to it. Is there a sort of flag that can deploy newly added set of functions only? Eg
firebase deploy --only functions --new
PS: I know I can specify the newly added functions to be deployed in the command. However most times it is hard to track the new functions and all I want to do is deploy newly added functions
No, such a flag is not available in the CLI.
You could maybe take advantage of the option that allows grouping functions into export groups in your /functions/index.js file. Grouping functions allows you to deploy multiple functions using a single command. In other words, you would create a new group for each set of new functions. See the CLI Reference for more details.

Will Firebase replace all my cloud functions?

In our organization, we have a repo for all cloud-functions. They are all in Python. We've had to create another cloud-function and we've decided to write it in NodeJS.
This new cloud-function is in a new repo, in a new project. There's no relationship between the old functions and the new one.
I've read that if I use firebase deploy --only functions, it will deploy all functions in the index.js file, but I'm not sure if it will remove the other ones, since there is nothing in my index.js file about the other cloud-functions written in Python.
Is it safe to deploy my new function with firebase deploy --only functions? I don't want to replace the other functions, just add my new function.
Thanks!
If you run firebase deploy or firebase deploy --only functions, Firebase will likely replace all the Cloud Functions in the project.
If you want to only deploy a single function, or a group of functions, you can specify that as an additional argument:
firebase deploy --only functions:yourNewFunction
Also see the Firebase documentation on deploying specific functions.
I just tested this out, whenever deploying functions by using firebase deploy or firebase deploy --only functions and the index.js does not find the name of a function it will display this message:
The following functions are found in your project but do not exist in your local source code:
<function_name>(<region>)
If you are renaming a function or changing its region, it is recommended that you create the new function first before deleting the old one to prevent event loss. For more info, visit https://firebase.google.com/docs/functions/manage-functions#modify
If a function has the same name as one of the Cloud Functions already deployed it will replace the older function. But if it has a different name it should not be replaced and you should be safe. If you still feel unsure feel free to test this out with quick start functions.

Firebase cloud function inline code editor

I'm trying to reduce the cold start time on my firebase cloud function. I have around 30 functions that use different imports.
As in an info video mentioned, it's better to use only the imports that your cloud function needs.
In the google cloud console, you can view your code.
But if I scroll down the LIB/INDEX.JS contains all my functions.
There's an option to edit the code.
Would it be harmful to delete all other functions & the imports that aren't used (for that specific function in LIB/INDEX.JS) with the inline code editor? (Even though I made my functions via typescript in visual studio code).
Thanks!
No, it would not be harmful to the other functions. If you are editing the code of a Cloud Function in the console, it will not modify the code used by other Cloud Functions. Each function is fully isolated from each other, even if they share the same deployment. The code is copied between each function.
That said, editing functions deployed by the Firebase CLI should only be done in experimentation. When it comes time to actually deploy code, you should again use the CLI to finalize everything.

Firebase functions in same project but multiple apps

In same Firebase project I have a node app with several functions and another app with only scheduled functions (because for some reason, I encountered side effects if deployed together in same app).
Each time I deploy the app with only scheduled functions, it tells me that other functions are not present in the source code (obviously) and asks me if I want to delete them.
Is there a way to tag functions as permanent and avoid each time to have to chose to not delete them ?
When you deploy Cloud Functions through the Firebase CLI, it expects that you pass it a index.js/index.ts that contains all the functions for that entire project.
There is no way to tag certain Cloud Functions as permanent. I usually explicitly tell Cloud Functions what functions I'm deploying in situations such as yours, with firebase deploy --only functions:function1,function2.
For more on this option, see the reference documentation on deploying specific functions. The option to group the functions sounds especially useful for your scenario, as you could group them by app.

How do you un-deploy Cloud Functions for Firebase?

I was experimenting with Cloud Functions for Firebase and deployed one of the samples in the functions-samples repo and linked it to an existing project using firebase use --add.
Since I was just playing around, these files were not added to my version control, and I no longer have access to the machine this was done on. I would like to build on this function or create new ones, but I am unsure how to either 1) modify and re-deploy over the original function or 2) un-use --add and start fresh. I see the function in my firebase console but no 'delete' or 'remove' button and have read through the CLI docs looking for clues to no avail.
Thanks for adding functions to the firebase 'stack.' Any guidance is much appreciated.
Functions get deleted when they aren't present during a firebase deploy. Most commonly that'd be because because you removed the function from your index.js, but if you've removed the whole functions directory that'll work too.
To continue work on a function for which you don't have the source anymore, the easiest is to start fresh. The function(s) you deploy will replace the ones you deployed previously.
Alternatively, if you're partial to using the Google Cloud Console instead of the Firebase Console, the Cloud Console will show you the code for the currently-deployed function, so you can copy-paste it onto your local machine.
The Cloud Console also has a 'delete' button for every function, and even a web editor. Be aware when editing functions from the Cloud Console though: your next firebase deploy will overwrite any changes.
To delete a function explicitly use the following command:
$ firebase functions:delete myFunction
Replace myFunction with your function name. For detailed info. follow this link: https://firebase.google.com/docs/functions/manage-functions
Go to Google Cloud Console Cloud Functions and select project, then select the function you want to undeploy or delete. Select it and click delete.
To delete/undeploy a single function, you can delete the code for your function and then run the following in command line:
firebase deploy --only functions:YourFunctionName
Replace YourFunctionName with the name of your function
In the case that you are working on other functions that you are not ready to deploy or do not want to deploy all of your functions for any reason, then the code above can be handy. This also feels a bit safer since you're not redeploying everything :)
Now you can simply delete a function from the function dashboard as follows. Click on the three dots mark of relevant function in the right side corner.

Resources