FastAPI CloudRun Starter: Serveless Backend Deployment
In the realm of web application development, the need for efficient and streamlined backend deployment is paramount. The FastAPI CloudRun Starter repository addresses this need by providing a robust and easy-to-use starter template for setting up a FastAPI backend using Google CloudRun. This tool harnesses the power of modern infrastructure-as-code principles to simplify the deployment process of FastAPI applications, making it an invaluable asset for developers.
https://github.com/mazzasaverio/fastapi-cloudrun-starter
Terraform-Based Infrastructure Setup
The integration of Terraform in the FastAPI CloudRun Starter allows for the automated setup of infrastructure. This Infrastructure as Code (IaC) tool enables developers to define and provision data center infrastructure using a high-level configuration language. This approach ensures consistency and repeatability across environments, significantly reducing the potential for human error during the setup process.
Seamless Integration with GitHub Actions
GitHub Actions facilitates continuous integration (CI) by automating workflows. In this template, GitHub Actions are configured to automatically test and deploy applications. This integration ensures that every code commit goes through a standardized testing process, enhancing code quality and reliability.
Automated Cloud Build Triggers
Google Cloud Build is utilized for automated building, testing, and deploying. The starter template sets up triggers that automatically initiate Cloud Build processes upon code changes, ensuring that the latest version of the application is always ready for deployment.
Secure Storage with Secret Manager
Security is a top priority in application deployment. The use of Google Cloud’s Secret Manager in this template ensures that sensitive information, such as API keys and credentials, is stored securely. This tool provides a centralized and secure way to manage and access secrets, reducing the risk of data breaches.
Getting Started
Prerequisites
Before diving into this template, certain prerequisites are necessary:
- Google Cloud Platform Account: An active GCP account is required. Sign up here if you don’t have one.
- Project Setup: Create a new GCP project and take note of the project ID for future steps.
- Service Account Creation: Set up a service account with ‘Owner’ permissions and generate a JSON key file for authentication.
- GitHub Integration: For integration with GitHub, generate a personal access token with the necessary permissions.

Terraform Configuration
Terraform plays a crucial role in this template. Start by renaming the terraform.tfvars.example to terraform.tfvars and input your credentials. This step initializes the Terraform configuration, tailoring it to your specific GCP setup.
Docker Configuration
The Dockerfile is pre-configured to use NVIDIA's CUDA base image, catering to FastAPI's dependencies. It exposes the application on port 8000, but this can be modified as per your requirements.
FastAPI Application Setup
At the heart of this template is the main.py script, which serves as the entry point for the FastAPI application. It comes with basic routes, but you're encouraged to extend and customize it according to your application's needs.
Usage and Deployment
Deploying your infrastructure and application is straightforward:
- Initialize Terraform with
terraform init. - Apply the Terraform configuration using
terraform apply. - To build and run the Docker container locally, execute
docker-compose up --build.
