0
votes

I am trying to deploy Windows VM on Google Cloud through terraform. The VM is getting deployed and I am able to execute PowerShell scripts by using windows-startup-script-url.

With this approach, I can only use scripts which are already stored in Google Storage. If the script has parameters / variables, then how to pass that parameter, any clue !

provider "google" {
  project = "my-project"
  region  = "my-location"
  zone    = "my-zone"
}

resource "google_compute_instance" "default" {
  name         = "my-name"
  machine_type = "n1-standard-2"
  zone         = "my-zone"

  boot_disk {
    initialize_params {
      image = "windows-cloud/windows-2019"
    }
  }

  metadata {
    windows-startup-script-url = "gs://<my-storage>/<my-script.ps1>"
  }


  network_interface {
    network = "default"

    access_config {

    }
  }

  tags = ["http-server", "windows-server"]
}


resource "google_compute_firewall" "http-server" {
  name    = "default-allow-http"
  network = "default"

  allow {
    protocol = "tcp"
    ports    = ["80"]
  }


  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["http-server"]
}

resource "google_compute_firewall" "windows-server" {
  name    = "windows-server"
  network = "default"

  allow {
    protocol = "tcp"
    ports    = ["3389"]
  }

  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["windows-server"]
}

output "ip" {
  value = "${google_compute_instance.default.network_interface.0.access_config.0.nat_ip}"
}
1

1 Answers

0
votes

Terraform doesn't require startup scripts to be pulled from GCS buckets necessarily.

The example here shows:

 }

  metadata = {
    foo = "bar"
  }

  metadata_startup_script = "echo hi > /test.txt"

  service_account {
    scopes = ["userinfo-email", "compute-ro", "storage-ro"]
  }
}

More in Official docs for GCE and Powershell scripting here