Python is a language that keeps getting more popular thanks to its convenience and ability to handle many different kinds of work. No more rambling, let's "make your hands dirty"!
Prerequisites
- Python3 installed
- pip installed: the PyPI Package Manager
- A BizflyCloud account
Preparation
Code
In this post I'll deploy a simple Python web app running the Flask framework — anyone who's worked with Python has definitely heard of this famous framework.
Let's create a file named main.py with the following content:
from flask import Flask
app = Flask(__name__)
@app.get("/")
def index():
return "This is your first Python app run on App Engine BizflyCloud!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=True)
In the code above, I'll run a Python web app on port 8080 with host 0.0.0.0, meaning it accepts access from any IP address into this webserver.
Note: In some cases, running with host=127.0.0.1 or localhost will cause an error when deploying onto App Engine, since it doesn't allow access from outside. The host config above is only suitable for a local dev environment.
I add a default route @app.get("/") to receive requests from visitors — if a request comes in, this python application returns This is your first Python app run on App Engine BizflyCloud!. Pretty easy to understand, right.
Next we need to install the Flask framework and Gunicorn to run this Python web app, with the command
pip install flask gunicorn
Let's try running the python application above with the following command:
python main.py
# or run using gunicorn
gunicorn --bind :8080 --workers 1 --threads 8 --timeout 0 main:app
Dependencies
BizflyCloud App Engine detects the Python language based on specific files used to manage dependencies, for example:
- requirements.txt
- Pipfile
- poetry.lock
- environment.yml
- ...
So your source code needs to contain these files for App Engine to detect the Python language and build accordingly. The simplest way to create this file is to run the command:
pip freeze > requirements.txt
The file's content once created will look like:
Flask==2.0.2
gunicorn==20.1.0
Run Command
You can fully configure the Run command, or the command used to run the application, for the Python web app on App Engine. To configure the Run Command you need to add a file named Procfile — here I'll run the application with gunicorn installed in the previous section. The Procfile will have this content:
# Run command
web: gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
Explanation
- web: defines the name of the process running this command
- gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app: Runs the Python web app with gunicorn, with the port taken from the PORT environment variable, using 1 worker with 8 threads, and a timeout of 0.
Learn more about gunicorn here: gunicorn.org
Python Version
Managing which Python version you'll run also matters quite a bit. To configure which Python version you'll run on BizflyCloud App Engine, create a runtime.txt file with the following content:
python-3.11.0
Supported Python versions get updated continuously at the docs link: docs.bizflycloud.vn/app_engine/deploy/python
Basically, your application can now be deployed. You can apply the steps above to python source code you already have, or use the sample source code I've pushed to Github
github.com/bizflycloud/app-engine-sample-python
Creating a Service
To create a service on App Engine you need a BizflyCloud account — I covered these steps in a previous post, check it out there!
In the create service section I'll fill in the fields with the content shown below:
- Service Name: the service's name, for reference
- Location: where the application gets deployed (the product is currently in beta, so only Hanoi is supported — more locations will come later)
- Source Type: Git - here I'm deploying from Github so I'll use GIT; if you've already packaged your application as an image, you can choose image
- Source URL: the link to the github repo (note this URL shouldn't contain the branch)
- Branch: the branch you want to deploy
In the application config section, since this is a Web service, I'll choose HTTP as the Application Type — the PORT here will be the port your service runs on; if your python web runs on port 8080 you'd set it to 8080, but since my Run command is currently set to run with the port equal to the PORT environment variable, this section doesn't need any change, defaulting to 80.
You can also add environment variables to the application to connect to databases, Message Queues, etc. in the Environment Config section
Once everything is configured, hit confirm to create the service. The deployment process happens right away — you can watch the packaging process logs in the Logs section in the left-side menu.
For this application, it only takes about 3 minutes, and the system deploys it successfully and returns a URL for you to access.
The returned link will look like example-nnwpu.appengine.bfcplatform.vn/python-app. Here's the result once accessed:
Wrapping up
Currently BizflyCloud App Engine lets you deploy 4 services, each up to 2 CPU - 4GB Ram, so you can comfortably deploy apps from light to heavy. What are you waiting for — go deploy your code onto App Engine right now.
Hope this post helped you out at work in some way. Any questions, feel free to comment and I'll answer, or reach me directly on Telegram @HoangViet12
And don't forget, if you found this post good, please Upvote and Follow me to see posts on deploying other kinds of Apps.
References
App Engine Docs: docs.bizflycloud.vn/app_engine