resource "google_compute_instance" "frontend" {
  name         = "frontend-${substr(var.zone, -1, 2)}"
  project      = var.project_id
  zone         = var.zone
  machine_type = var.instance_type
  labels       = merge({tier = "frontend"}, var.labels)

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
      size  = 10
      type  = "pd-standard"
    }
  }

  network_interface {
    network    = var.network
    subnetwork = var.frontend_subnet_id
    access_config {}
  }

  metadata = {
    enable-oslogin = "TRUE"
    ssh-keys       = var.ssh_pub_key
  }

  tags = ["frontend", "ssh"]

  service_account {
    email  = var.service_account_email
    scopes = ["userinfo-email", "compute-ro"]
  }
}

resource "google_compute_instance" "backend" {
  name         = "backend-${substr(var.zone, -1, 2)}"
  project      = var.project_id
  zone         = var.zone
  machine_type = var.instance_type
  labels       = merge({tier = "backend"}, var.labels)

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
      size  = 10
      type  = "pd-standard"
    }
  }

  network_interface {
    network    = var.network
    subnetwork = var.backend_subnet_id
  }

  metadata = {
    enable-oslogin = "TRUE"
    ssh-keys       = var.ssh_pub_key
  }

  tags = ["backend", "ssh"]

  service_account {
    email  = var.service_account_email
    scopes = ["userinfo-email", "compute-ro"]
  }
}

resource "google_compute_instance" "database" {
  name         = "database-${substr(var.zone, -1, 2)}"
  project      = var.project_id
  zone         = var.zone
  machine_type = var.instance_type
  labels       = merge({tier = "database"}, var.labels)

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
      size  = 20
      type  = "pd-standard"
    }
  }

  network_interface {
    network    = var.network
    subnetwork = var.database_subnet_id
  }

  metadata = {
    enable-oslogin = "TRUE"
    ssh-keys       = var.ssh_pub_key
  }

  tags = ["database", "ssh"]

  service_account {
    email  = var.service_account_email
    scopes = ["userinfo-email", "compute-ro"]
  }
}