From 2d4eb6ed7403f515a68b6669ea16db85f7c039d6 Mon Sep 17 00:00:00 2001 From: gastonchenet Date: Thu, 4 Dec 2025 11:43:33 +0100 Subject: [PATCH] Bonus --- terraform/environments/dev/main.tf | 1 + terraform/environments/dev/variables.tf | 6 ++++ terraform/modules/compute/main.tf | 48 ++++++++++++++++++++++++- terraform/modules/compute/variables.tf | 5 +++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/terraform/environments/dev/main.tf b/terraform/environments/dev/main.tf index fdc93c1..76129cf 100644 --- a/terraform/environments/dev/main.tf +++ b/terraform/environments/dev/main.tf @@ -51,4 +51,5 @@ module "compute" { database_subnet_id = module.network.subnet_ids["database"] ssh_pub_key = var.ssh_pub_key service_account_email = module.iam.service_account_email + instance_count = var.compute_instance_count } \ No newline at end of file diff --git a/terraform/environments/dev/variables.tf b/terraform/environments/dev/variables.tf index efec7da..fa95373 100644 --- a/terraform/environments/dev/variables.tf +++ b/terraform/environments/dev/variables.tf @@ -26,4 +26,10 @@ variable "instance_type" { type = string description = "Type of the VM instance" default = "e2-small" +} + +variable "compute_instance_count" { + type = number + description = "Number of VM instances to create" + default = 2 } \ No newline at end of file diff --git a/terraform/modules/compute/main.tf b/terraform/modules/compute/main.tf index ad02eb3..3614b0f 100644 --- a/terraform/modules/compute/main.tf +++ b/terraform/modules/compute/main.tf @@ -96,4 +96,50 @@ resource "google_compute_instance" "database" { email = var.service_account_email scopes = ["userinfo-email", "compute-ro"] } -} \ No newline at end of file +} + +resource "google_compute_instance_template" "frontend_template" { + name = "${var.project_id}-frontend-template" + project = var.project_id + machine_type = var.instance_type + + tags = ["frontend", "ssh"] + labels = merge({ tier = "frontend" }, var.labels) + + metadata = { + enable-oslogin = "TRUE" + ssh-keys = var.ssh_pub_key + custom-value = "custom-metadata" + } + + disk { + auto_delete = true + boot = true + source_image = "debian-cloud/debian-11" + } + + network_interface { + network = var.network_id + subnetwork = var.frontend_subnet_id + access_config {} + } + + service_account { + email = var.service_account_email + scopes = ["userinfo-email", "compute-ro"] + } +} + + +resource "google_compute_region_instance_group_manager" "frontend_mig" { + name = "${var.project_id}-frontend-mig" + project = var.project_id + region = var.region + base_instance_name = "frontend" + + version { + instance_template = google_compute_instance_template.frontend_template.self_link + } + + target_size = var.instance_count +} diff --git a/terraform/modules/compute/variables.tf b/terraform/modules/compute/variables.tf index f200d93..ac69abc 100644 --- a/terraform/modules/compute/variables.tf +++ b/terraform/modules/compute/variables.tf @@ -53,4 +53,9 @@ variable "service_account_email" { variable "labels" { type = map(string) default = {} +} + +variable "instance_count" { + type = number + description = "Number of VM instances to create" } \ No newline at end of file