I understand that Google Cloud Functions is a serverless architecture to run functions and I have gone through the documentation of Cloud Functions. I want to run an entire Flask app with CRUD APIs on Cloud Functions. I saw several articles on running Python Functions on Cloud Functions. Can someone help me find an article/tutorial on how to run a Flask app through a single Cloud Function?
-
3It's not recommended. Cloud Functions is for deploying a single function. If you need to deploy an entire web application, Cloud Run gives you a much better experience and has very similar behavior/controls/pricing.Dustin Ingram– Dustin Ingram2020-06-30 20:48:08 +00:00Commented Jun 30, 2020 at 20:48
2 Answers
If you have a flask application, I recommend you to use Cloud Run. It's very similar to Cloud Functions (in reality it's the same backend) but you can run a container.
I have wrote an article on What I use and What I prefer between Cloud Functions and Cloud Run.
If your flask app is standard, you can use a standard Dockerfile to build it. Change the pip install line (or add another one) to import the dependencies of your project
If you haven't Docker installed on your computer, you can use cloud build like this
gcloud builds submit -t gcr.io/<PROJECT_ID>/<CONTAINER_NAME>
And then, deploy on Cloud Run
gcloud run deploy --image gcr.io/<PROJECT_ID>/<CONTAINER_NAME> --platform=managed
4 Comments
If the requirement is 'single cloud function serving multiple routes' and not necessarily 'Flask/Python' (and you're happy writing Javascript), then a cloud function with an Express server is very well supported:
https://firebase.google.com/docs/functions/http-events#using_existing_express_apps
(I suspect this is not what you're looking for, but other users might find this helpful)