/gcp-cloud-run-fastapi

GCP Cloud Run Fast API quick demo

Primary LanguagePython

FastAPI Cloud Run Demo ๐Ÿš€

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.

๐ŸŽฏ What This Demo Shows

  • 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

๐Ÿ“ Project Structure

fastapi-cloud-run-demo/
โ”œโ”€โ”€ main.py              # FastAPI application
โ”œโ”€โ”€ requirements.txt     # Python dependencies
โ”œโ”€โ”€ Dockerfile          # Container configuration
โ”œโ”€โ”€ .dockerignore       # Docker ignore patterns
โ””โ”€โ”€ README.md          # This file

๐Ÿ›  Prerequisites

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

Enable Required APIs

bash

gcloud services enable cloudbuild.googleapis.com run.googleapis.com

๐Ÿš€ Quick Start

Option 1: Deploy Directly (Recommended)

  1. Clone this repository

    bash

    git clone https://github.com/rishabkumar7/gcp-cloud-run-fastapi
    cd gcp-cloud-run-fastapi
  2. Set your Google Cloud project

    bash

    gcloud config set project YOUR_PROJECT_ID
  3. 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.

Option 2: Build Then Deploy

  1. Build the container image

    bash

    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/fastapi-demo
  2. 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

๐Ÿ” API Endpoints

Once deployed, your app will have these endpoints:

  • GET / - Welcome message
  • GET /health - Health check endpoint
  • GET /api/data - Sample data endpoint
  • GET /docs - Interactive API documentation (Swagger UI)

๐Ÿงช Local Development

Run with Python

bash

pip install -r requirements.txt
uvicorn main:app --reload --port 8080

Run with Docker

bash

docker build -t fastapi-demo .
docker run -p 8080:8080 fastapi-demo

Visit http://localhost:8080 to see your app running locally.

โš™๏ธ Configuration Options

Environment Variables

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=production

Custom Domain

To use a custom domain:

bash

gcloud run domain-mappings create \
  --service fastapi-demo \
  --domain your-domain.com \
  --region us-central1

Traffic Splitting

Deploy 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=50

๐Ÿ“Š Monitoring and Logs

View logs

bash

# Real-time logs
gcloud logs tail "resource.type=cloud_run_revision"

# Recent logs
gcloud logs read "resource.type=cloud_run_revision" --limit 50

Metrics

Visit the Google Cloud Console to see:

  • Request count and latency
  • Instance count and CPU/memory usage
  • Error rates
  • Cost breakdown

๐Ÿ›ก๏ธ Security Features

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

๐Ÿ’ฐ Cost Optimization

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

๐Ÿšจ Troubleshooting

Common Issues

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 PORT environment variable (Cloud Run sets this automatically)
  • Review logs with gcloud logs read

Getting Help

๐ŸŽค Talk Resources

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

๐Ÿค Contributing

Feel free to submit issues, fork the repository, and create pull requests for any improvements.

๐Ÿ“„ License

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! ๐ŸŒŸ