diff --git a/infra/environments/artifacts/main.tf b/infra/environments/artifacts/main.tf new file mode 100644 index 0000000..5bb258a --- /dev/null +++ b/infra/environments/artifacts/main.tf @@ -0,0 +1,30 @@ + +terraform { + backend "gcs" { + bucket = "infra-new-state" + prefix = "artifacts" + } + required_providers { + google = { + source = "hashicorp/google" + version = "~> 5.0" + } + } +} + +provider "google" { + project = "launchflow-services-dev" + region = "us-west1" +} + +resource "google_artifact_registry_repository" "app" { + location = "us-west1" + repository_id = "app" + description = "Docker repository for application images" + format = "DOCKER" +} + +output "repository_path" { + description = "The full path to the Artifact Registry repository" + value = "${google_artifact_registry_repository.app.location}-docker.pkg.dev/${google_artifact_registry_repository.app.project}/${google_artifact_registry_repository.app.repository_id}" +} diff --git a/infra/environments/dev/main.tf b/infra/environments/dev/main.tf new file mode 100644 index 0000000..88b7b55 --- /dev/null +++ b/infra/environments/dev/main.tf @@ -0,0 +1,38 @@ + +terraform { + backend "gcs" { + bucket = "infra-new-state" + prefix = "dev" + } + required_providers { + google = { + source = "hashicorp/google" + version = "~> 5.0" + } + } +} + +provider "google" { + project = "launchflow-services-dev" + region = "us-west1" +} + +# Reference the artifact registry for the image +data "google_artifact_registry_repository" "app" { + location = "us-west1" + repository_id = "app" +} + +module "cloud_run_service" { + source = "../../modules/cloud-run" + + project_id = "launchflow-services-dev" + location = "us-west1" + service_name = "app-dev" + image = "${data.google_artifact_registry_repository.app.location}-docker.pkg.dev/${data.google_artifact_registry_repository.app.project}/${data.google_artifact_registry_repository.app.repository_id}/service:latest" +} + +output "service_url" { + description = "The URL of the deployed service" + value = module.cloud_run_service.service_url +} diff --git a/infra/modules/cloud-run/main.tf b/infra/modules/cloud-run/main.tf new file mode 100644 index 0000000..3e22483 --- /dev/null +++ b/infra/modules/cloud-run/main.tf @@ -0,0 +1,39 @@ + +module "cloud_run" { + source = "GoogleCloudPlatform/cloud-run/google" + version = "0.17.2" + + # Required variables + project_id = var.project_id + location = var.location + service_name = var.service_name + image = var.image + + # Optional configurations + service_account_email = google_service_account.service_account.email + + template_annotations = { + "run.googleapis.com/client-name" = "terraform" + "generated-by" = "terraform" + "autoscaling.knative.dev/maxScale" = "4" + "autoscaling.knative.dev/minScale" = "1" + } + + service_annotations = { + "run.googleapis.com/ingress" = "all" + } +} + +# Create a service account for the Cloud Run service +resource "google_service_account" "service_account" { + project = var.project_id + account_id = "${var.service_name}-sa" + display_name = "Service Account for ${var.service_name} Cloud Run service" +} + +# Grant the service account access to GCS +resource "google_project_iam_member" "gcs_access" { + project = var.project_id + role = "roles/storage.objectViewer" + member = "serviceAccount:${google_service_account.service_account.email}" +} diff --git a/infra/modules/cloud-run/outputs.tf b/infra/modules/cloud-run/outputs.tf new file mode 100644 index 0000000..60f7d07 --- /dev/null +++ b/infra/modules/cloud-run/outputs.tf @@ -0,0 +1,15 @@ + +output "service_url" { + description = "The URL of the deployed service" + value = module.cloud_run.service_url +} + +output "service_account_email" { + description = "The email of the service account" + value = google_service_account.service_account.email +} + +output "service_name" { + description = "The name of the Cloud Run service" + value = module.cloud_run.service_name +} diff --git a/infra/modules/cloud-run/variables.tf b/infra/modules/cloud-run/variables.tf new file mode 100644 index 0000000..a9ff209 --- /dev/null +++ b/infra/modules/cloud-run/variables.tf @@ -0,0 +1,20 @@ + +variable "project_id" { + description = "The project ID to deploy to" + type = string +} + +variable "location" { + description = "The location to deploy to" + type = string +} + +variable "service_name" { + description = "The name of the Cloud Run service" + type = string +} + +variable "image" { + description = "The container image to deploy" + type = string +}