Skip to content

Commit

Permalink
UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
mazzasaverio committed Feb 6, 2024
1 parent d41df7d commit 973af17
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@


code_report.py
custom_tree_and_files_corrected.txt

# .tfstate files
*.tfstate
Expand Down
98 changes: 98 additions & 0 deletions FastAPI_CloudRun_Starter_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

# FastAPI CloudRun Starter

This repository serves as a starter template for setting up a FastAPI backend using Google CloudRun. It's designed to streamline the process of deploying a FastAPI application using modern infrastructure as code principles.

## Features

- Terraform-based infrastructure setup.
- Integration with GitHub Actions for continuous integration.
- Automatic trigger setup for Cloud Build.
- Secure storage of secrets using Secret Manager.

## Project Structure

```
/home/sam/github/fastapi-cloudrun-starter
├── archive
│ ├── docker-compose.yml
│ └── src
│ └── app.py
├── cloudbuild.yaml
├── code_report.py
├── custom_tree_and_files_corrected.txt
├── docker-compose.dev.yml
├── Dockerfile
├── .dockerignore
├── .env.dev
├── .env.example
├── .github
│ └── workflows
│ └── docker-image.yml
├── .gitignore
├── main.py
├── README.md
├── requirements.txt
└── terraform
├── main.tf
├── modules
│ ├── cloud_build
│ │ ├── main.tf
│ │ └── variables.tf
│ ├── cloud_run
│ │ ├── main.tf
│ │ ├── output.tf
│ │ └── variables.tf
│ └── secret_manager
│ ├── main.tf
│ └── variables.tf
├── terraform copy.example
├── .terraform.lock.hcl
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
└── variabiles.tf
```

## Getting Started

### Prerequisites

1. **Google Cloud Platform Account**: Ensure you have an active GCP account. [Sign up here](https://cloud.google.com/) if needed.
2. **Project Setup**: Create a new GCP project and note down the project ID.
3. **Service Account**: Create a service account with 'Owner' permissions in your GCP project and generate a JSON key file.
4. **Connecting Cloud Build to Your GitHub Account**: Create a personal access token in GitHub with `repo` and `read:user` permissions. For organization apps, include `read:org` permission. [Guide here](https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen#terraform_1).

### Terraform Configuration

- **Rename File**: Rename `terraform.tfvars.example` to `terraform.tfvars`.
- **Insert Credentials**: Fill in your credentials in the `terraform.tfvars` file.

### Docker Configuration

The `Dockerfile` is configured to use the NVIDIA CUDA base image with FastAPI dependencies. The application is exposed on port 8000 and can be customized as needed.

### FastAPI Application

The `main.py` script is the entry point for the FastAPI application. It includes basic routes and can be extended for additional functionality.

## Usage

To deploy the infrastructure and application:

1. Initialize Terraform:
```bash
terraform init
```
2. Apply Terraform configuration:
```bash
terraform apply
```
3. To build and run the Docker container locally, use:
```bash
docker-compose up --build
```

## Contributing

Contributions to enhance this starter template are welcome. Please follow standard GitHub contribution guidelines.
148 changes: 91 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,97 @@
# Cloud Run Docker Image Template

This repository provides a Docker image template designed for deployment using Google Cloud's Cloud Build and to be triggered by Cloud Run whenever files are uploaded to storage.

## Overview

When storage dispatches a notification, it is captured through pub/sub and triggers two tests:

1. Read and write operations on a Cloud SQL PostgreSQL database.
2. Storing a pre-trained model from Hugging Face in the storage used as cache, which is then read and utilized each time the Cloud Run is triggered.

FastAPI is used to handle pub/sub requests. For setting up infrastructure components like Cloud SQL, Cloud Build, and Cloud Run, etc., please refer to another repository where you'll find the specific Terraform code for this use case.

## Key Functions

The main functionalities – database connectivity with read/write operations and text translation using a pre-trained model fetched from Hugging Face – are all triggered in the cloud when a message from pub/sub arrives. However, for local testing and independent execution, these functions can be run directly from the terminal.

## Local Testing

To test the `/pubsub-handler` endpoint locally:

1. Start the FastAPI server with the following command:

# FastAPI CloudRun Starter

This repository serves as a starter template for setting up a FastAPI backend using Google CloudRun. It's designed to streamline the process of deploying a FastAPI application using modern infrastructure as code principles.

## Features

- Terraform-based infrastructure setup.
- Integration with GitHub Actions for continuous integration.
- Automatic trigger setup for Cloud Build.
- Secure storage of secrets using Secret Manager.

## Project Structure

```
/home/sam/github/fastapi-cloudrun-starter
├── archive
│ ├── docker-compose.yml
│ └── src
│ └── app.py
├── cloudbuild.yaml
├── code_report.py
├── custom_tree_and_files_corrected.txt
├── docker-compose.dev.yml
├── Dockerfile
├── .dockerignore
├── .env.dev
├── .env.example
├── .github
│ └── workflows
│ └── docker-image.yml
├── .gitignore
├── main.py
├── README.md
├── requirements.txt
└── terraform
├── main.tf
├── modules
│ ├── cloud_build
│ │ ├── main.tf
│ │ └── variables.tf
│ ├── cloud_run
│ │ ├── main.tf
│ │ ├── output.tf
│ │ └── variables.tf
│ └── secret_manager
│ ├── main.tf
│ └── variables.tf
├── terraform copy.example
├── .terraform.lock.hcl
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
└── variabiles.tf
```

## Getting Started

### Prerequisites

1. **Google Cloud Platform Account**: Ensure you have an active GCP account. [Sign up here](https://cloud.google.com/) if needed.
2. **Project Setup**: Create a new GCP project and note down the project ID.
3. **Service Account**: Create a service account with 'Owner' permissions in your GCP project and generate a JSON key file.
4. **Connecting Cloud Build to Your GitHub Account**: Create a personal access token in GitHub with `repo` and `read:user` permissions. For organization apps, include `read:org` permission. [Guide here](https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen#terraform_1).

### Terraform Configuration

- **Rename File**: Rename `terraform.tfvars.example` to `terraform.tfvars`.
- **Insert Credentials**: Fill in your credentials in the `terraform.tfvars` file.

### Docker Configuration

The `Dockerfile` is configured to use the NVIDIA CUDA base image with FastAPI dependencies. The application is exposed on port 8000 and can be customized as needed.

### FastAPI Application

The `main.py` script is the entry point for the FastAPI application. It includes basic routes and can be extended for additional functionality.

## Usage

To deploy the infrastructure and application:

1. Initialize Terraform:
```bash
uvicorn src.main:app --reload
terraform init
```

This command will host the application on a local server, usually available at `http://127.0.0.1:8000`.

2. Send a POST request to your locally running server. You can use Postman or a simple curl command:
2. Apply Terraform configuration:
```bash
curl -X POST http://127.0.0.1:8000/pubsub-handler
terraform apply
```
3. To build and run the Docker container locally, use:
```bash
docker-compose up --build
```

Dev

docker compose -f docker-compose.dev.yml up --build

## Prerequisites

### 1. Google Cloud Platform Account

- **Sign Up**: Ensure you have an active GCP account. [Sign up here](https://cloud.google.com/) if needed.

### 2. Project Setup

- **New Project**: Create a new GCP project. Note down the project ID for future use.

### 3. Service Account

- **Create Service Account**: Create a service account with 'Owner' permissions in your GCP project.
- **Generate Key File**: Generate a JSON key file for this service account and store it securely.

### 5. Connecting Cloud Build to Your GitHub Account

- Create a personal access token. Make sure to set your token (classic) to have no expiration date and select the following permissions when prompted in GitHub: repo and read:user. If your app is installed in an organization, make sure to also select the read:org permission.

https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen#terraform_1

## Terraform Configuration
## Contributing

- **Rename File**: Change `terraform.tfvars.example` to `terraform.tfvars`.
- **Insert Credentials**: Add your credentials to the `terraform.tfvars` file.
Contributions to enhance this starter template are welcome. Please follow standard GitHub contribution guidelines.
2 changes: 2 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ resource "google_project_iam_member" "cloud_build_service_account_iam_roles" {
}



/* -------------------------------------------------------------------------- */
/* Modules */
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -94,3 +95,4 @@ module "cloud_build" {
module.secret_manager
]
}

34 changes: 34 additions & 0 deletions terraform/modules/cloud_run/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
resource "google_cloud_run_v2_service" "default" {
name = "cloudrun-service"
location = "us-central1"
ingress = "INGRESS_TRAFFIC_ALL"

template {
containers {
image = "gcr.io/${var.gcp_project_id}/fastapi-cloudrun:latest"
resources {
limits = {
cpu = "2"
memory = "1024Mi"
}
}
}

# Include other necessary configurations such as scaling, vpc_access, etc.
}

# Traffic configuration
traffic {
type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
percent = 100
}

# Additional configurations as needed
}

resource "google_cloud_run_service_iam_member" "public_invoker" {
location = "us-central1"
service = google_cloud_run_v2_service.default.name
role = "roles/run.invoker"
member = "allUsers"
}
3 changes: 3 additions & 0 deletions terraform/modules/cloud_run/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "service_url" {
value = google_cloud_run_v2_service.default.uri
}
15 changes: 15 additions & 0 deletions terraform/modules/cloud_run/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "gcp_region" {
description = "The region where the Cloud Run service will be deployed."
type = string
}

variable "network_id" {
description = "The ID of the VPC network."
type = string
}

variable "gcp_project_id" {
description = "Project ID"
type = string
}

28 changes: 28 additions & 0 deletions terraform/terraform copy.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ------------------------------ GCP Foundation----------------------------- */

gcp_project_id = "gcp_project_id"
gcp_project_number = "gcp_project_number"
gcp_credentials_file = "~/gcp_credentials_file.json"
gcp_service_account_name = "gcp_service_account_name"

gcp_services = ["cloudbuild","run","compute"]

gcp_existing_service_account_roles = [
"secretmanager.secretAccessor",
]
gcp_cloud_build_service_account_roles = ["secretmanager.secretAccessor","iam.serviceAccountUser", "compute.admin", "run.admin"]


/* ------------------------------ GCP Network ----------------------------- */
gcp_network_name = "default"
gcp_region = "us-central1"
gcp_zone = "us-central1-c"


/* ------------------------------- Cloud Build ------------------------------ */
repo_name = "fastapi-cloudrun-starter"
branch = "master"

github_token = "github_token"
github_gcp_installation_id = "github_gcp_installation_id"
github_remote_uri = "github_remote_uri"

0 comments on commit 973af17

Please sign in to comment.