A simple FastAPI application demonstrating how easy it is to deploy to Google Cloud Run. This repo accompanies the lightning talk "Why I Love Google Cloud Run" and shows how to go from code to production in just a few commands.
- Simple FastAPI app with multiple endpoints
- Containerized deployment using Docker
- One-command deployment to Google Cloud Run
- Production-ready with automatic HTTPS, scaling, and monitoring
fastapi-cloud-run-demo/
โโโ main.py # FastAPI application
โโโ requirements.txt # Python dependencies
โโโ Dockerfile # Container configuration
โโโ .dockerignore # Docker ignore patterns
โโโ README.md # This file
Before you start, make sure you have:
- Google Cloud CLI installed and configured
- Docker installed (optional, for local testing)
- A Google Cloud Project with billing enabled
- Cloud Run and Cloud Build APIs enabled
bash
gcloud services enable cloudbuild.googleapis.com run.googleapis.com-
Clone this repository
bash
git clone https://github.com/rishabkumar7/gcp-cloud-run-fastapi cd gcp-cloud-run-fastapi -
Set your Google Cloud project
bash
gcloud config set project YOUR_PROJECT_ID -
Build and deploy in one command
bash
gcloud run deploy fastapi-demo \ --source . \ --platform managed \ --region us-central1 \ --allow-unauthenticated
That's it! ๐ Your app will be live at the URL shown in the output.
-
Build the container image
bash
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/fastapi-demo
-
Deploy to Cloud Run
bash
gcloud run deploy fastapi-demo \ --image gcr.io/YOUR_PROJECT_ID/fastapi-demo \ --platform managed \ --region us-central1 \ --allow-unauthenticated
Once deployed, your app will have these endpoints:
GET /- Welcome messageGET /health- Health check endpointGET /api/data- Sample data endpointGET /docs- Interactive API documentation (Swagger UI)
bash
pip install -r requirements.txt
uvicorn main:app --reload --port 8080bash
docker build -t fastapi-demo .
docker run -p 8080:8080 fastapi-demoVisit http://localhost:8080 to see your app running locally.
You can set environment variables during deployment:
bash
gcloud run deploy fastapi-demo \
--image gcr.io/YOUR_PROJECT_ID/fastapi-demo \
--set-env-vars DEBUG=false,ENVIRONMENT=productionTo use a custom domain:
bash
gcloud run domain-mappings create \
--service fastapi-demo \
--domain your-domain.com \
--region us-central1Deploy new versions with gradual traffic shifting:
bash
gcloud run deploy fastapi-demo \
--image gcr.io/YOUR_PROJECT_ID/fastapi-demo:v2 \
--traffic latest=50,previous=50bash
# Real-time logs
gcloud logs tail "resource.type=cloud_run_revision"
# Recent logs
gcloud logs read "resource.type=cloud_run_revision" --limit 50Visit the Google Cloud Console to see:
- Request count and latency
- Instance count and CPU/memory usage
- Error rates
- Cost breakdown
This demo automatically includes:
- โ HTTPS by default - All traffic is encrypted
- โ Container security scanning - Images are scanned for vulnerabilities
- โ IAM integration - Fine-grained access control
- โ VPC connectivity - Can connect to private resources
- โ Secret management - Secure environment variable handling
Cloud Run is cost-effective because:
- Pay per request - No idle costs
- Scales to zero - No traffic = no charges
- 100ms billing - Only pay for actual usage
- Free tier - 2 million requests/month free
Build fails:
- Check that Docker is installed and running
- Verify your project ID is correct
- Ensure Cloud Build API is enabled
Deploy fails:
- Check that Cloud Run API is enabled
- Verify you have sufficient IAM permissions
- Try a different region if capacity issues
App doesn't start:
- Ensure your app listens on
0.0.0.0:8080 - Check the
PORTenvironment variable (Cloud Run sets this automatically) - Review logs with
gcloud logs read
This repository was created for the lightning talk "Why I Love Google Cloud Run".
Key takeaways:
- โก From code to production in < 5 minutes
- ๐ฏ Perfect for APIs, web apps, and microservices
- ๐ก Focus on code, not infrastructure
- ๐ Free tier (based on us-central1 active pricing):
- CPU - First 180,000 vCPU-seconds free per month
- RAM - First 360,000 GiB-seconds free per month
- Requests - 2 million requests free per month
Feel free to submit issues, fork the repository, and create pull requests for any improvements.
This project is licensed under the MIT License - see the LICENSE file for details.
Ready to try Cloud Run? Star this repo and deploy your first app! ๐