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.
Where can a Flask app live?
| Kind | Examples | You manage | Good for |
|---|---|---|---|
| Platform as a service (PaaS) | Render, Railway, Fly.io, Heroku, PythonAnywhere | just your code or container | most student and small projects |
| Serverless containers | Google Cloud Run, Azure Container Apps, AWS (for example ECS with Fargate) | a container image | apps with uneven traffic; scale to zero when idle |
| ML demo hosts | Hugging Face Spaces (Docker, Gradio or Streamlit) | a repo | sharing a model demo publicly |
| Virtual machine | AWS EC2, Azure VMs, DigitalOcean droplets | everything: OS, updates, web server, security | learning 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: 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.
Deploying a container, step by step
- Push your code to GitHub with the Dockerfile,
requirements.txtand tests (and without secrets). - 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.
- Set environment variables in the platform's dashboard:
SECRET_KEY,DATABASE_URL, API keys. The platform also setsPORT; the Dockerfile above listens on it. - Add a managed database (PostgreSQL) from the same provider, and run your migrations (
flask db upgrade) as a release step. - Set the health check path to
/health, so the platform restarts the app if it stops answering. - 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.
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.
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?
📚 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.
- Google Cloud Run: deploy a Python service ↗ an official quickstart for serverless containers
- The Twelve-Factor App ↗ principles for apps that deploy cleanly