π₯ Why Deploy a Flask App on Google Cloud?
If you’re looking to deploy a Flask app on Google Cloud in a secure, scalable, and cost-efficient way β this guide is your one-stop resource.
Whether you’re building a startup MVP or launching a side project, youβll learn how to:
- Deploy your Flask backend using Cloud Run
- Automate everything with CI/CD from GitHub
- Secure your site with HTTPS via Cloudflare
- Use Uptime checks and monitoring
- Stay within Google Cloud’s free tier limits
And yes β it costs you $0 if done right.
π§° Tools & Services Used
Tool/Service | Purpose |
---|---|
Google Cloud Run | Serverless hosting for Python web apps |
GitHub + Cloud Build | CI/CD automation |
Cloudflare (Free Tier) | DNS, SSL, HTTP headers, basic WAF |
Flask (Python) | Lightweight backend framework |
π Live Demo
π See the live demo of the deployed Flask application.
ποΈ Project Structure
bashCopyEditflask-app/
βββ main.py
βββ requirements.txt
βββ Dockerfile
βββ cloudbuild.yaml
βββ tests/
βββ test_app.py
Focus Keyphrase in Image Alt:
β Step-by-Step: Deploy Flask App on Google Cloud
1. βοΈ Initialize Flask Project
Create main.py
:
pythonCopyEditfrom flask import Flask
import os
app = Flask(__name__)
@app.route("/")
def home():
return "Coming Soon"
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8080))
app.run(host="0.0.0.0", port=port)
Set up requirements.txt
:
txtCopyEditFlask==2.3.3
2. π³ Add Dockerfile
dockerfileCopyEditFROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY main.py .
CMD ["python", "main.py"]
EXPOSE 8080
3. π οΈ Enable Google Cloud Services
- Create a GCP project
- Enable Cloud Run, Cloud Build, IAM, and Artifact Registry
- Push code to GitHub
- Link GitHub β Google Cloud β Cloud Build
4. β‘ Add CI/CD with Cloud Build
Hereβs the cloudbuild.yaml
:
yamlCopyEditsteps:
- name: 'python:3.9'
entrypoint: 'bash'
args:
- '-c'
- |
pip install -r requirements.txt
pytest || echo "Tests failed"
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: 'bash'
args:
- '-c'
- |
gcloud run deploy flask-app \
--source . \
--region us-central1 \
--platform managed \
--allow-unauthenticated
options:
logging: CLOUD_LOGGING_ONLY
timeout: 900s
5. π Setup Cloudflare + Domain Mapping
Add to Cloudflare DNS:
Type | Name | Content |
---|---|---|
A | @ | 216.239.32.21, .34.21, .36.21, .38.21 |
AAAA | @ | Google IPv6s |
CNAME | www | ghs.googlehosted.com. |
Then go to Cloud Run > Domain Mapping β Map your domain.
6. π Add HTTPS and Security Headers
Use Cloudflare Transform Rules and Managed Transforms to add:
Strict-Transport-Security
Content-Security-Policy
Permissions-Policy
Also enable:
- β Bot Fight Mode
- β SSL/TLS Full (Strict)
7. π Monitor Uptime + Logs
Set up:
- Uptime Check for
https://www.example-flask-app.com
- Alert policy for
uptime_check/check_passed == 0
- Review Audit Logs for IAM/service usage
π§ͺ Testing
Use pytest
and create a basic test in tests/test_app.py
:
pythonCopyEditfrom main import app
def test_home():
client = app.test_client()
response = client.get('/')
assert response.status_code == 200
assert b"Coming Soon" in response.data
π§Ύ Summary Checklist
β Task | Status |
---|---|
Deploy Flask App on Google Cloud | β |
CI/CD with GitHub | β |
Domain + SSL (Cloudflare) | β |
Uptime Monitoring | β |
Security Headers via Transform | β |
Free Tier Optimization | β |
π Related Resources
π¬ Final Thoughts
This is a powerful starter stack forΒ deploying a Flask app on Google Cloud securely and efficiently, without spending a dime.
Next steps:
- Add a frontend (e.g., React or HTML templates)
- Connect Firebase or Firestore
- Add user authentication