πŸš€ How to Deploy a Flask App on Google Cloud for Free (with SSL, CI/CD & Monitoring)

Deploy a Flask App on Google Cloud Deploy a Flask App on Google Cloud

πŸ”₯ 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/ServicePurpose
Google Cloud RunServerless hosting for Python web apps
GitHub + Cloud BuildCI/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:

TypeNameContent
A@216.239.32.21, .34.21, .36.21, .38.21
AAAA@Google IPv6s
CNAMEwwwghs.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

βœ… TaskStatus
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

Leave a Reply

Your email address will not be published. Required fields are marked *