Terraform GCP – Remote Execute PowerShell in Windows Instance on Create

google-cloud-platformpowershellterraform

I have made a simple Terraform manifest that successfully initiates a Windows 2016 instance in GCP. As a next step I would like to execute a Powershell script to further configure and install software in the Windows instance.

I can not for my life find any example on how to execute a remote script through Terraform in a Windows GCP instance. For Linux instances it seems pretty straight forward, but how is it supposed to be done for Windows? Does anyone know any examples?

Any thoughts or ideas are much appreciated, thanks!

Best Answer

You can set the metadata to init your script like this:

resource "google_compute_instance" "default" {
 project = "your_project"
 zone = "us-central1-c"
 name = "tf-windows-script"
 machine_type = "f1-micro"
 boot_disk {
   initialize_params {
     image = "windows-server-2008-r2-dc-v20180710"
   }
 }
 network_interface {
   network = "default"
   access_config {
   }
 }
 metadata {
    windows-startup-script-url = "gs://your_bucket/startup.ps1"
  }
}

In my test the init script works without problems,

Here you can check the documentation of how to provide a startup script for Windows instances