Flask & Deployment
Topic 9 · Shipping to production · ⏱ about 3 h

Deploying to the Cloud

The kinds of hosting (platforms, serverless containers, ML-demo hosts, virtual machines), deploying a container step by step, environment variables and managed databases, HTTPS and custom domains, scaling, cost control, and continuous deployment.

Show:
In this topic
Beginner

Where can a Flask app live?

KindExamplesYou manageGood for
Platform as a service (PaaS)Render, Railway, Fly.io, Heroku, PythonAnywherejust your code or containermost student and small projects
Serverless containersGoogle Cloud Run, Azure Container Apps, AWS (for example ECS with Fargate)a container imageapps with uneven traffic; scale to zero when idle
ML demo hostsHugging Face Spaces (Docker, Gradio or Streamlit)a reposharing a model demo publicly
Virtual machineAWS EC2, Azure VMs, DigitalOcean dropletseverything: OS, updates, web server, securitylearning operations, special needs

Free tiers and prices change often (some free services sleep after inactivity, making the first request slow), so check each provider's current pricing page. Pick a region close to your users, for example Sydney for Australian users, and consider where personal data is allowed to be stored.

Try it

Try it: which host fits?

Pick the most sensible starting point for each project. Several could work; the explanation says why one fits best.

Interactive: this part runs in your browser.

Beginner

Deploying a container, step by step

  1. Push your code to GitHub with the Dockerfile, requirements.txt and tests (and without secrets).
  2. Create a service on the platform and point it at the repository (most PaaS build the Dockerfile for you), or build and push the image to a registry yourself.
  3. Set environment variables in the platform's dashboard: SECRET_KEY, DATABASE_URL, API keys. The platform also sets PORT; the Dockerfile above listens on it.
  4. Add a managed database (PostgreSQL) from the same provider, and run your migrations (flask db upgrade) as a release step.
  5. Set the health check path to /health, so the platform restarts the app if it stops answering.
  6. Deploy, open the URL, and check the logs.

For example, on Google Cloud Run a single command builds and deploys from the current folder: gcloud run deploy clinic-app --source . --region australia-southeast1.

Beginner

HTTPS, domains, scaling and cost

  • HTTPS is automatic on almost every platform. Never send logins or personal data over plain HTTP.
  • Custom domain: buy a domain, then add the DNS record the platform tells you (usually a CNAME), and it issues the certificate.
  • Scaling: more gunicorn workers use more of one machine's CPU; more instances (copies of the container) handle more traffic. This only works if the app is stateless: keep data in the database and files in object storage (such as S3 or Cloud Storage), never on the container's own disk, which disappears on restart.
  • Cost control: set a budget alert in the cloud console on day one, cap the maximum number of instances, delete experiments you no longer use, and rate-limit public endpoints, especially ones that call paid LLM APIs.
  • Continuous deployment: most platforms redeploy automatically when the main branch changes, ideally after the CI tests pass.
Check yourself

Quiz

1. Why must a horizontally scaled app be stateless?

2. The first request to your free-tier app takes 30 seconds, then it is fast. A likely reason?

3. What is the first thing to set up when you open a cloud account?

Go deeper

📚 Further reading

Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.

Ready