A template for deploying a FastAPI application on Google Cloud Run.
- Cloud Logging: Integration with Google Cloud Logging, ensuring logs are captured with the appropriate severity levels. ⭐ Credits to Florian Flock's article.
- CI/CD: Automated deployment using GitHub Actions. 🚀
- HTTP Async Support: Built-in support for async HTTP requests using httpx for better performance and scalability.
- API Key Authentication: Support for API Key authentication through both Query Parameters and HTTP Headers.
- Python 3.12+
- Docker
-
Create a virtual environment: You can follow the FastAPI guide.
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
fastapi dev
Deployment is automated with GitHub Actions, but you'll need to set up a few items first:
-
GCP Project: Of course!
-
Workload Identity Federation: This allows GitHub to authenticate to your GCP project for deployment. This video is a helpful resource.
-
Google Artifact Registry Repository: This is where Docker images will be stored.
Set the following secrets in your GitHub repository (do not expose these publicly):
SERVICE_NAME: Name of your service, e.g.,fastapi-cloud-run.GCP_PROJECT_ID: Your project ID, e.g.,9876543210.GCP_WIF_PROVIDER: Your Workload Identity Federation provider full name, formatted asprojects/${PROJECT_ID}/locations/global/workloadIdentityPools/${POOL_NAME}/providers/${PROVIDER_NAME}.GCP_WIF_SA_EMAIL: Service account email to impersonate.
The deployment workflow will:
- Run Ruff checks for code quality
- Build and push the Docker image to Google Artifact Registry
- Deploy the service to Cloud Run in the specified region
Refer to .github/workflows/push_main.yml for more details on the deployment process.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.