This repository demonstrates how to deploy a simple Python Flask web application to Google Cloud Platform services, including App Engine and Cloud Run.
- ✅ Running local Python source code.
- ✅ Building and running Docker locally.
- ✅ Deploying Flask App to Cloud Run / App Engine from source.
- ✅ Deploying Flask App to App Engine using a standard instance.
- ✅ Deploying Flask App to App Engine using a flexible environment (Docker).
- ✅ Building, pushing, and deploying Flask App to Cloud Run using Cloud Build.
-
Create a local Python environment at
.venv
using Python 3.11 and the requirements specified inhello_app/requirements.txt
.- TIPS: If you use VS Code, press
Cmd+Shift+P
, searchPython: Create Environment...
, and follow the prompts.- Select Python 3.11.xxx.xxx
- Check the suggested requirements.txt.
- Restart you terminal (
Ctrl+Shift+
).
- Alternatively, you can use the
make
commands s1 and s2.
- TIPS: If you use VS Code, press
-
Set up your
gcloud cli
, You can find the installation steps here -
Activate the appropriate GCP project using commands like:
gcloud config configurations list
gcloud cofig configurations activate YOUR_CONFIG_NAME
-
Run
make
in the terminal to view the existing rules.You will get a list of commands to support quick runs.
Rule Description s1_create_environment Step 1: Create python virtual environment s2_requirements Step 2: Install requirements s3_test Step 3: Local Test s4_local_run Step 4: Local Run s5_deploy_to_app_engine_from_source Step 5: Deploy the Model to App Engine from Source s6_deploy_to_cloud_run_from_source Step 6: Deploy the Model to Cloud Run from Source s7_local_docker_build_and_run Step 7: Local docker build and run s8_deploy_to_app_engine_using_docker Step 8: Deploy to App Engine using docker s9_deploy_to_cloud_run_using_docker Step 9: Deploy to Cloud Run using docker
Note
- Find more details in
makefile
. - The default COMPUTE_DEFAULT_REGION is
australia-southeast1
, modify the code to suit your gcloud settings. - Remember to Enable google Cloud Run and App Engine services for s5-s9; and create service account for the best practice.
After setting the venv
, start the flask app by the following cmd:
make make s4_local_run
You should see results like
source .venv/bin/activate && python hello_app/main.py
* Serving Flask app 'main'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http:https://127.0.0.1:8080
...
Press CTRL+C to quit
127.0.0.1 - - [20/May/2024 10:23:42] "GET / HTTP/1.1" 200 -
You can copy and paste the following links to test the flask app:
Page/Func | URL |
---|---|
Home | http:https://127.0.0.1:8080/ |
About | http:https://127.0.0.1:8080/about/ |
Contact | http:https://127.0.0.1:8080/contact/ |
Hello | http:https://127.0.0.1:8080/hello/YOUR_NAME |
API | http:https://127.0.0.1:8080/api/data |
- Install the Google Cloud CLI
- App Engine documentation
- App Engine standard environment
- App Engine Flexible Environment
- Cloud Run documentation
- Quickstart: Deploy a Python service to Cloud Run
- Build applications or websites quickly on a fully managed platform
- Cloud Build
- Cloud Build Trigger / CI/CD
- Deploying to Cloud Run using Cloud Build
- Deploying to App Engine
END