Skip to content

Commit d8bc916

Browse files
committed
Add provision of Google Cloud Run using Docker container and Artifact Registry storing the Fast API app Docker image.
1 parent 1d4f4c1 commit d8bc916

File tree

8 files changed

+168
-37
lines changed

8 files changed

+168
-37
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
poetry.lock
22
~
3-
3+
.terraform
44
# Byte-compiled / optimized / DLL files
55
__pycache__/
66
*.py[cod]

infra/README.md

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,28 @@
1-
# Introduction
1+
# Google
22

3-
Terraform is infrastructure as code tool. The infrastructure we are making here contains a Docker registry and a Function app amongst other resources.
4-
The Function App uses a Docker container which needs to be available first, so we build and push it in the next section. We might also choose to create a DevOps pipeline to do this for us.
5-
Build and push Docker container so that it can be used in function app.
3+
To deploy the infra on Google, first authenticate:
64

7-
## How to run
5+
`gcloud auth application-default login`
86

9-
### Docker
7+
Set the project id:
108

11-
Build:
9+
`gcloud config set project <project_id>`
1210

13-
`docker build -f Dockerfile -t embedding_service:latest . `
11+
Enable gcloud service 'artifactregistry.googleapis.com':
1412

13+
`gcloud services enable artifactregistry.googleapis.com`
1514

16-
Login:
15+
`terraform init`
1716

18-
`az acr login --name EmbeddingContainerRegistry `
17+
`terraform plan`
1918

20-
Tag:
21-
22-
`docker tag embedding_service:latest embeddingcontainerregistry.azurecr.io/embedding_service:latest`
23-
24-
Push:
25-
26-
`docker push embeddingcontainerregistry.azurecr.io/embedding_service:latest`
27-
28-
### Terraform
29-
30-
Login:
31-
32-
`az login`
33-
34-
Navigate to the infra folder:
35-
36-
`cd infra`
37-
38-
Init:
39-
40-
`terraform init`
41-
42-
Plan:
19+
`terraform apply`
4320

44-
`terraform plan`
21+
## Push Docker container
4522

4623

47-
Apply:
24+
`gcloud auth configure-docker europe-west4-docker.pkg.dev`
4825

49-
`terraform apply`
26+
## Github Actions
5027

28+
The infrastructure can also be deployed using Github Actions

infra/azure/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Introduction
2+
3+
Terraform is infrastructure as code tool. The infrastructure we are making here contains a Docker registry and a Function app amongst other resources.
4+
The Function App uses a Docker container which needs to be available first, so we build and push it in the next section. We might also choose to create a DevOps pipeline to do this for us.
5+
Build and push Docker container so that it can be used in function app.
6+
7+
## How to run
8+
9+
### Docker
10+
11+
Build:
12+
13+
`docker build -f Dockerfile -t embedding_service:latest . `
14+
15+
16+
Login:
17+
18+
`az acr login --name EmbeddingContainerRegistry `
19+
20+
Tag:
21+
22+
`docker tag embedding_service:latest embeddingcontainerregistry.azurecr.io/embedding_service:latest`
23+
24+
Push:
25+
26+
`docker push embeddingcontainerregistry.azurecr.io/embedding_service:latest`
27+
28+
### Terraform
29+
30+
Login:
31+
32+
`az login`
33+
34+
Navigate to the infra folder:
35+
36+
`cd infra`
37+
38+
Init:
39+
40+
`terraform init`
41+
42+
Plan:
43+
44+
`terraform plan`
45+
46+
47+
Apply:
48+
49+
`terraform apply`
50+
File renamed without changes.
File renamed without changes.

infra/gcp/main.tf

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
variable "project_id" {
2+
description = "project id"
3+
type = string
4+
default = "fastapi-449213"
5+
}
6+
7+
variable "project" {
8+
description = "project"
9+
type = string
10+
default = "fastapi"
11+
}
12+
13+
14+
variable "region" {
15+
description = "The GCP region"
16+
type = string
17+
default = "europe-west4"
18+
}
19+
20+
21+
provider "google" {
22+
project = var.project_id
23+
region = var.region
24+
}
25+
26+
27+
# Enable necessary APIs
28+
resource "google_project_service" "container_registry" {
29+
for_each = toset([
30+
"container.googleapis.com",
31+
"run.googleapis.com",
32+
"artifactregistry.googleapis.com"
33+
])
34+
project = var.project_id
35+
service = each.key
36+
}
37+
38+
resource "google_project_service" "artifact_registry_api" {
39+
service = "artifactregistry.googleapis.com"
40+
project = var.project_id
41+
}
42+
43+
# Container Registry: Images are stored in Artifact Registry
44+
resource "google_artifact_registry_repository" "container_registry" {
45+
repository_id = "fastapi-docker-repo"
46+
format = "DOCKER"
47+
location = var.region
48+
description = "Docker repository for FastAPI images"
49+
}
50+
51+
# IAM Binding for Artifact Registry
52+
resource "google_artifact_registry_repository_iam_binding" "artifact_registry_binding" {
53+
repository = google_artifact_registry_repository.container_registry.name
54+
role = "roles/artifactregistry.writer"
55+
members = ["serviceAccount:${google_service_account.cloud_run_service_account.email}"]
56+
}
57+
58+
# Service Account for Cloud Run
59+
resource "google_service_account" "cloud_run_service_account" {
60+
account_id = "cloud-run-service-account"
61+
display_name = "Cloud Run Service Account"
62+
}
63+
64+
# Grant necessary roles to the Service Account
65+
resource "google_project_iam_binding" "cloud_run_iam" {
66+
project = var.project_id
67+
role = "roles/run.admin"
68+
members = ["serviceAccount:${google_service_account.cloud_run_service_account.email}"]
69+
}
70+
71+
# Deploy FastAPI Docker app to Cloud Run
72+
resource "google_cloud_run_service" "fastapi_service" {
73+
name = "fastapi-service"
74+
location = var.region
75+
76+
template {
77+
spec {
78+
containers {
79+
image = "${google_artifact_registry_repository.container_registry.location}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.container_registry.name}/fastapi:latest"
80+
resources {
81+
limits = {
82+
memory = "512Mi"
83+
cpu = "1"
84+
}
85+
}
86+
}
87+
service_account_name = google_service_account.cloud_run_service_account.email
88+
}
89+
}
90+
91+
traffic {
92+
percent = 100
93+
latest_revision = true
94+
}
95+
}
96+
97+
# Grant permissions to Cloud Run Invoker
98+
resource "google_cloud_run_service_iam_binding" "invoker_permission" {
99+
service = google_cloud_run_service.fastapi_service.name
100+
location = var.region
101+
role = "roles/run.invoker"
102+
members = ["allUsers"] # Allows public access; modify as needed
103+
}

0 commit comments

Comments
 (0)