Firebase cloud functions using Python? - python

We are using GCP's Firebase with Firestore for a new mobile app we are developing. As part of this effort we need to deploy a number of cloud functions which will act as Firestore triggers for doing some back end processing.
Our intention is to keep the deploys encapsulated inside of Firebase by using the firebase cli tools. However when we attempt to initiate the Firebase project for functions using the "firebase init functions" call the only two language options are "Javascript" and "Typescript", and the only deployable stack seems to be Node.js.
On previous GCP projects we had deployed Python based cloud functions (using the gcloud cli) and ideally we'd like to continue using Python for our Firebase cloud functions. So my questions are:
is it possible to deploy Python-based Firebase cloud functions? If not:
can we simply go back to deploying Python-based GCP cloud functions using the gcloud cli and still have them work as Firestore triggers?
Thanks

The Firebase CLI does not support deploying functions written in python.
You can certainly write Cloud Firestore triggers in python and deploy them with gcloud.
One thing you might not be aware of: the underlying Cloud Functions product is the same no matter how you deploy your functions. Firebase just adds tools and APIs on top of the existing Google Cloud Functions infrastructure. There is really no such thing as a "Firebase Cloud Function". There is just Cloud Functions, and you have options about how you can write and deploy them, either using gcloud, or the Firebase CLI.

Related

Python firebase_admin vs. google.cloud packages

I am trying to get Firestore working and I am wondering which Python package I should use. It appears that there is some overlap between the functionality of the firebase_admin.firestore and google.cloud.firestore, as well as between firebase_admin.credentials and google.auth.credentials. But it also seems like there is some incompatibility between them, or at least they can't be used together.
What is the difference between these Python packages, and which is it recommended that a beginner should use?
Thanks!
Here’s a simple answer for beginners, as requested:
Google provides Firebase and Google Cloud Platform as two different suites of products. Some Google products are shared across these, Cloud Firestore being one of them.
So, if one is using Cloud Firestore from within a Firebase project, it is recommended to use firebase_admin.firestore and firebase_admin.credentials packages and other Firebase APIs as required.
If on the other hand, one is using the Google Cloud Platform project, it is recommended to use google.cloud.firestore, and google.auth.credentials packages and other GCP Cloud APIs as required.
HTH, reach out for any additional questions/queries.
If you plan to use other Firebase functionality (e.g. eventually have mobile users that authenticate with Firebase, and connect to their Firestore instance from their phones or web pages), then firebase_admin is the best choice.
Otherwise, if you plan on using Firestore without Firebase, then google.cloud.firestore would be more straight-forward.
The same applies to the credentials libraries.
And the main differences are between the Cloud services:
Cloud Firestore supports SDKs for Android, IOS, and Web. Combined with Cloud Firestore security rules and Firebase Auth, the mobile and web SDKs support serverless app architectures where clients connect directly to your Cloud Firestore database. With a serverless architecture, you do not need to maintain an intermediary server between your clients and your Cloud Firestore database.
The Firebase Admin SDKs bundle the Google Cloud client libraries for Cloud Firestore alongside client libraries and SDKs for several other Firebase features. And it is for accessing your Firebase products on a backend server you control, which could be Cloud Functions, or even your desktop. It will typically have full access to everything, as determined by the service account you used to initialize it.

Running Python Backend Firebase

I am new to using firebase so I was looking to see if it was possible to call a python script in firebase to manipulate an image.
I have already written a python program that takes an input image and processes it and returns a new image. I would like to extend this where a mobile or web app could send a user-provided image, to the database, the python program would run and the app would show the processed image.
I assume that a REST API could handle this but my question is if I could run the python script over the cloud. I know firebase has a cloud run function but I have never touched it. Please let me know if there are any resources for this or any alternatives.
Although Firebase Functions can be written only in JS or TS they are part of Google Cloud Function. If you create the function in Firebase you will see it as well in Functions in Google Cloud Console.
So it's practically the same feature, however GCP gives you possibility to write cloud functions in many languages (currently 7, I think). Among them you can use Python
in version 3.7, 3.8 and 3.9 (currently in beta).
You can use GCP GUI to write the functions or use Cloud SDK (gcloud functions deploy) to create the function. Nice examples are in mentioned documentation.
Additionally the Cloud Function has nice choice of triggers including Firebase related.

How to authenticate a google vision API on cloud inside a python script?

I am trying to build a python script and deploy it as an HTTP function/Serverless cloud function on Pivotal cloud foundry or GKE, but I have gone through several articles and most of them mention using an SA and download Json key, setup env variable to JSON key location and run the script.
But how can I provide local downloaded JSON key file when I deploy it on cloud?
I have gone through below links but I couldn't understand as I am new to GCP, can anyone provide me an elaborated anws on how can I achieve this?
Google Cloud Vision API - Python
Google cloud vision api- OCR
https://cloud.google.com/vision/docs/quickstart-client-libraries#client-libraries-usage-python
According to docs, during function execution, Cloud Functions uses the service account PROJECT_ID#appspot.gserviceaccount.com as its identity. For instance, when making requests to Google Cloud Platform services using the Google Cloud Client Libraries, Cloud Functions can automatically obtain and use tokens to authorize the services this identity has permissions to use.
By default, the runtime service account has the Editor role, which lets it access many GCP services. In your case, you will need to enable the Vision API and grant the default service account with necessary permissions. Check out the Function Identity Docs for more details.
Instead of a SA json file, you could use an api key if that's easier for you. However, if you use an api key, you will only be able to send image bytes or specify a public image.

Flask: Difference between google cloud functions and google web deploy

I am a newbie who wants to deploy his flask app using google cloud functions. When I am searching it online, people are telling me to deploy it as a Flask app. I want to ask if there is any difference between those two.
A cloud instance or deploying flask app on google cloud VS cloud serverless function
As described by John and Kolban, Cloud Functions is a single purpose endpoint. You want to perform 1 thing, deploy 1 function.
However, if you want to have a many consistent things, like a microservice, you will have to deploy several endpoints that allow you to perform a CRUD on the same data object. You should prefer to deploy several endpoints (CRUD) and to have the capability to easily reuse class and object definitions and business logic. For this, a Flask webserver is that I recommend (and I prefer, I wrote an article on this).
A packaging in Cloud Run is the best for having a serverless platform and pay-per-use pricing model (and automatic scaling and...).
There is an additional great thing: Cloud Functions request object is based on Flask request object. By the way, and it's that I also present in my article, it's easy to switch from one platform to another one. You only have to choose according with your requirements, your skills,... I also wrote another article on this
If you deploy your Flask app as an application in a Compute Engine VM instance, you are basically configuring a computer and application to run your code. The notion of Cloud Functions relieves you from the chore and toil of having to create and manage the environment in which your program runs. A marketing mantra is "You bring the code, we bring the environment". When using Cloud Functions all you need do is code your application logic. The maintenance of the server, scaling up as load increases, making sure the server is available and much more is taken care of for you. When you run your code in your own VM instance, it is your responsibility to manage the whole environment.
References:
HTTP Functions
Deploying a Python serverless function in minutes with GCP

appengine frontend to kubernetes

I'm trying to setup a flask app on google app engine that will be something of a frontend management console for google container engine. Google has put out working APIs to spin up a container cluster, but it does not look like they have put out (python) APIs to administer kubernetes. That is, everything needed implement services, pods, RCs etc. seems to be setup to run through bash scripting. This is not compatible with the restrictions of google's app engine.
Is there a commonly accepted solution/package for this? Would it make more sense to abandon appengine in favor for a managed VM (not ideal)?
Thanks
As I mentioned in Submit jobs using API Client Library for Python?, the Kubernetes API uses a standard swagger specification, so it should be possible to generate a python client library. There is also pykube if you want to experiment with a existing client library.

Categories

Resources