Skip to content

Commit

Permalink
Merge pull request #5 from witek3100/deployment
Browse files Browse the repository at this point in the history
Deployment
  • Loading branch information
witek3100 authored Mar 19, 2024
2 parents 93136c4 + c42969d commit f870bf5
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 10 deletions.
10 changes: 4 additions & 6 deletions Dockerfile
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}
1 change: 1 addition & 0 deletions infra/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export GOOGLE_APPLICATION_CREDENTIALS="infra_service_account.json"
155 changes: 155 additions & 0 deletions infra/main.tf
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
}
29 changes: 29 additions & 0 deletions infra/variables.tf
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"
}
8 changes: 4 additions & 4 deletions src/configs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
"api_key": ""
},
"pinecone": {
"api_key": "c079a1cd-1698-41b2-b457-74aa836db1aa",
"api_key": "",
"index_name": "chat-agh",
"environment": "gcp-starter"
},
"sources_loader": {
"chunk_size": 1200,
"chunk_overlap": 300
"chunk_size": 800,
"chunk_overlap": 100
},
"mongo": {
"uri": "mongodb://127.0.0.1:27017",
"uri": "mongodb+srv://admin:admin@chat-agh-db.75uebyb.mongodb.net",
"db": "chatagh"
}
}

0 comments on commit f870bf5

Please sign in to comment.