-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1547b0
commit 9eb59b4
Showing
2 changed files
with
62 additions
and
167 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
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,161 +1,66 @@ | ||
# Provider and Common Variables | ||
variable "project" { | ||
default = "automateml" | ||
} | ||
variable "region" { | ||
default = "us-east1" | ||
} | ||
variable "zone" { | ||
default = "us-east1-a" | ||
} | ||
variable "project" { default = "automateml" } | ||
variable "region" { default = "us-east1" } | ||
variable "zone" { default = "us-east1-a" } | ||
variable "sa_email" { default = "default@automateml.iam.gserviceaccount.com" } | ||
|
||
provider "google" { | ||
project = var.project | ||
region = var.region | ||
credentials = file("${path.module}/automateml-3f20c67d2a0a.json") | ||
credentials = file("credentials.json") //github actions creates ./cloud-infra/credentials.json | ||
zone = var.zone | ||
} | ||
|
||
# Add GKE Service Account | ||
# Minimum roles bc will be the default account used by requests | ||
resource "google_service_account" "GKE_tf_account" { | ||
account_id = "gke-tf-service-account" | ||
display_name = "A Service Account For Terraform To Make GKE Cluster" | ||
} | ||
|
||
# Kubernetes Version | ||
variable "cluster_version" { | ||
default = "1.26" | ||
} | ||
|
||
# Setup Clusters | ||
resource "google_container_cluster" "cluster" { | ||
name = "trail" | ||
location = var.zone | ||
min_master_version = var.cluster_version | ||
project = var.project | ||
|
||
# Ignore changes to min-master-version - this is bc version may be different to what TF expects | ||
lifecycle { | ||
ignore_changes = [ | ||
min_master_version, | ||
] | ||
} | ||
resource "google_container_cluster" "automl_cluster" { | ||
name = "automl-cluster" | ||
location = var.region | ||
project = var.project | ||
|
||
# Cant create cluster w/o pool defined, create smallest possible pool, delete immediatly | ||
# Use seperately managed pools | ||
remove_default_node_pool = true | ||
initial_node_count = 1 | ||
|
||
# Enable Workload Identity | ||
# allows workloads in clusters to impersonate IAM service accounts | ||
workload_identity_config { | ||
workload_pool = "${var.project}.svc.id.goog" | ||
} | ||
initial_node_count = 2 | ||
} | ||
|
||
# Node Pool Definition | ||
resource "google_container_node_pool" "primary_preemptible_nodes" { | ||
name = "trial-zero-cluster-node-pool" | ||
location = var.zone | ||
project = var.project | ||
cluster = google_container_cluster.cluster.name | ||
//single node "node pool" for frontend and backend pods | ||
resource "google_container_node_pool" "febe_node_pool" { | ||
name = "frontend-backend-node-pool" | ||
location = var.region | ||
cluster = google_container_cluster.automl_cluster.name | ||
node_count = 1 | ||
|
||
# Setup autoscaling with min and max number of nodes | ||
autoscaling { | ||
min_node_count = 1 | ||
max_node_count = 5 | ||
} | ||
|
||
version = var.cluster_version | ||
|
||
# Node configuration definition: | ||
node_config { | ||
|
||
preemptible = true | ||
machine_type = "e2-medium" | ||
|
||
# Google recommends custom service accounts that have cloud-platform scope and | ||
# permissions granted via IAM Roles. | ||
|
||
# Tie nodes to sa created above | ||
service_account = google_service_account.GKE_tf_account.email | ||
oauth_scopes = [ | ||
"https://www.googleapis.com/auth/cloud-platform" | ||
] | ||
|
||
# Disable regeneration of node pool everytime we run this file | ||
metadata = { | ||
disable-legacy-endpoints = "true" | ||
service_account = var.sa_email | ||
machine_type = "e2-micro" | ||
disk_size_gb = 30 | ||
labels = { | ||
workload_type = "frontend-backend" | ||
} | ||
} | ||
|
||
lifecycle { | ||
ignore_changes = [ | ||
# Ignore changes to node_count, initial_node_count and version | ||
# otherwise node pool will be recreated if there is drift between what | ||
# terraform expects and what it sees | ||
initial_node_count, | ||
node_count, | ||
version | ||
oauth_scopes = [ | ||
"https://www.googleapis.com/auth/cloud-platform", | ||
"https://www.googleapis.com/auth/logging.write", | ||
"https://www.googleapis.com/auth/monitoring" | ||
] | ||
} | ||
} | ||
|
||
//pool for machine learning (allows us to adjust the compute later if needed) | ||
resource "google_container_node_pool" "ml_node_pool" { | ||
name = "machine-learning-node-pool" | ||
location = var.region | ||
cluster = google_container_cluster.automl_cluster.name | ||
node_count = 1 | ||
|
||
# Older code: | ||
/* | ||
resource "google_service_account_iam_member" "GKE_account_iam" { | ||
service_account_id = google_service_account.GKE_account.name | ||
role = "roles/iam.serviceAccountUser" | ||
member = "user:jane@example.com" | ||
} | ||
# Allow SA service account use the default GCE account | ||
resource "google_service_account_iam_member" "gce-default-account-iam" { | ||
service_account_id = data.google_compute_default_service_account.default.name | ||
role = "roles/iam.serviceAccountUser" | ||
member = "serviceAccount:${google_service_account.sa.email}" | ||
} | ||
resource "google_project_service" "run_api" { | ||
provider = google | ||
service = "run.googleapis.com" | ||
disable_on_destroy = true | ||
} | ||
# Create the Cloud Run service | ||
resource "google_cloud_run_service" "run_service" { | ||
provider = google | ||
name = "automate" | ||
location = "us-east1" | ||
template { | ||
spec { | ||
containers { | ||
image = "us-east1-docker.pkg.dev/automateml/docker-repo/automate:1.0" | ||
} | ||
node_config { | ||
service_account = var.sa_email | ||
machine_type = "e2-micro" | ||
disk_size_gb = 30 | ||
labels = { | ||
workload_type = "machine-learning" | ||
} | ||
} | ||
|
||
traffic { | ||
percent = 100 | ||
latest_revision = true | ||
oauth_scopes = [ | ||
"https://www.googleapis.com/auth/cloud-platform", | ||
"https://www.googleapis.com/auth/logging.write", | ||
"https://www.googleapis.com/auth/monitoring" | ||
] | ||
} | ||
# Waits for the Cloud Run API to be enabled | ||
depends_on = [google_project_service.run_api] | ||
} | ||
resource "google_cloud_run_service_iam_member" "run_all_users" { | ||
provider = google | ||
service = google_cloud_run_service.run_service.name | ||
location = google_cloud_run_service.run_service.location | ||
role = "roles/run.invoker" | ||
member = "allUsers" | ||
} | ||
output "service_url" { | ||
value = google_cloud_run_service.run_service.status.0.url | ||
} | ||
*/ |