-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from witek3100/deployment
Deployment
- Loading branch information
Showing
5 changed files
with
193 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
FROM python:3.9-slim-buster | ||
FROM python:3.12 | ||
|
||
WORKDIR /code | ||
COPY /src /code | ||
COPY /src /code/src | ||
|
||
RUN pip install -r configs/requirements.txt | ||
RUN pip install -r src/configs/requirements.txt | ||
|
||
WORKDIR /web_app | ||
|
||
CMD gunicorn app:app -w 2 --threads 2 -b 0.0.0.0:${PORT} | ||
CMD gunicorn src.web-app.app:app -w 2 --threads 2 -b 0.0.0.0:${PORT} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export GOOGLE_APPLICATION_CREDENTIALS="infra_service_account.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
terraform { | ||
required_providers { | ||
google = { | ||
source = "hashicorp/google-beta" | ||
version = "4.25.0" | ||
} | ||
} | ||
} | ||
|
||
provider "google" { | ||
project = var.project_id | ||
region = var.region | ||
zone = var.zone | ||
} | ||
|
||
# Enable IAM API | ||
resource "google_project_service" "iam" { | ||
provider = google | ||
service = "iam.googleapis.com" | ||
disable_on_destroy = false | ||
} | ||
|
||
# Enable Artifact Registry API | ||
resource "google_project_service" "artifactregistry" { | ||
provider = google | ||
service = "artifactregistry.googleapis.com" | ||
disable_on_destroy = false | ||
} | ||
|
||
############################################# | ||
# Enable API's # | ||
############################################# | ||
|
||
# Enable Cloud Run API | ||
resource "google_project_service" "cloudrun" { | ||
provider = google | ||
service = "run.googleapis.com" | ||
disable_on_destroy = false | ||
} | ||
|
||
# Enable Cloud Resource Manager API | ||
resource "google_project_service" "resourcemanager" { | ||
provider = google | ||
service = "cloudresourcemanager.googleapis.com" | ||
disable_on_destroy = false | ||
} | ||
|
||
resource "time_sleep" "wait_30_seconds" { | ||
create_duration = "30s" | ||
depends_on = [ | ||
google_project_service.iam, | ||
google_project_service.artifactregistry, | ||
google_project_service.cloudrun, | ||
google_project_service.resourcemanager | ||
] | ||
} | ||
|
||
############################################# | ||
# Google Artifact Registry Repository # | ||
############################################# | ||
|
||
# Create Artifact Registry Repository for Docker containers | ||
resource "google_artifact_registry_repository" "my_docker_repo" { | ||
provider = google | ||
|
||
location = var.region | ||
repository_id = var.repository | ||
description = "My docker repository" | ||
format = "DOCKER" | ||
depends_on = [time_sleep.wait_30_seconds] | ||
} | ||
|
||
# Create a service account | ||
resource "google_service_account" "docker_pusher" { | ||
provider = google | ||
|
||
account_id = "docker-pusher" | ||
display_name = "Docker Container Pusher" | ||
depends_on =[time_sleep.wait_30_seconds] | ||
} | ||
|
||
# Give service account permission to push to the Artifact Registry Repository | ||
resource "google_artifact_registry_repository_iam_member" "docker_pusher_iam" { | ||
provider = google | ||
|
||
location = google_artifact_registry_repository.my_docker_repo.location | ||
repository = google_artifact_registry_repository.my_docker_repo.repository_id | ||
role = "roles/artifactregistry.writer" | ||
member = "serviceAccount:${google_service_account.docker_pusher.email}" | ||
depends_on = [ | ||
google_artifact_registry_repository.my_docker_repo, | ||
google_service_account.docker_pusher | ||
] | ||
} | ||
|
||
|
||
############################################## | ||
# Deploy API to Google Cloud Run # | ||
############################################## | ||
|
||
# Deploy image to Cloud Run | ||
resource "google_cloud_run_service" "api_test" { | ||
provider = google | ||
name = "api-test" | ||
location = var.region | ||
template { | ||
spec { | ||
containers { | ||
image = "europe-west4-docker.pkg.dev/${var.project_id}/${var.repository}/${var.docker_image}" | ||
resources { | ||
limits = { | ||
"memory" = "1G" | ||
"cpu" = "1" | ||
} | ||
} | ||
} | ||
} | ||
metadata { | ||
annotations = { | ||
"autoscaling.knative.dev/minScale" = "0" | ||
"autoscaling.knative.dev/maxScale" = "1" | ||
} | ||
} | ||
} | ||
traffic { | ||
percent = 100 | ||
latest_revision = true | ||
} | ||
depends_on = [google_artifact_registry_repository_iam_member.docker_pusher_iam] | ||
} | ||
|
||
# Create a policy that allows all users to invoke the API | ||
data "google_iam_policy" "noauth" { | ||
provider = google | ||
binding { | ||
role = "roles/run.invoker" | ||
members = [ | ||
"allUsers", | ||
] | ||
} | ||
} | ||
|
||
# Apply the no-authentication policy to our Cloud Run Service. | ||
resource "google_cloud_run_service_iam_policy" "noauth" { | ||
provider = google | ||
location = var.region | ||
project = var.project_id | ||
service = google_cloud_run_service.api_test.name | ||
|
||
policy_data = data.google_iam_policy.noauth.policy_data | ||
} | ||
|
||
output "cloud_run_instance_url" { | ||
value = google_cloud_run_service.api_test.status.0.url | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
variable "project_id" { | ||
description = "The name of the project" | ||
type = string | ||
default = "chat-agh" | ||
} | ||
|
||
variable "region" { | ||
description = "The default compute region" | ||
type = string | ||
default = "europe-west4" | ||
} | ||
|
||
variable "zone" { | ||
description = "The default compute zone" | ||
type = string | ||
default = "europe-west4-a" | ||
} | ||
|
||
variable "repository" { | ||
description = "The name of the Artifact Registry repository to be created" | ||
type = string | ||
default = "docker-repository" | ||
} | ||
|
||
variable "docker_image" { | ||
description = "The name of the Docker image in the Artifact Registry repository to be deployed to Cloud Run" | ||
type = string | ||
default = "my-api" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters