bluethundr
bluethundr

Reputation: 1345

How do I attach drives and assign drive letters to windows servers in GCP using Terraform?

I have a requirement to attach drives to a windows server VM in GCP and this has to be done in terraform. I am using terraform version 12.

We have 3 database servers that we need to get into terraform. The existing servers have drives mapped like this:

Data:   E
Log:    F
Backup: G

Currently the servers that I am building have the drives attached in this incorrect order and have the wrong letters assigned:

Log:    D
Backup: E
Data:   F

This is the code that I am using to create and attach the volumes:

// Create Data Disk
resource "google_compute_disk" "datadisk_instance1" {
  name  = var.data_disk_name_instance1
  type  = var.disk_type
  size  = var.data_disk_size
  zone  = var.zone1
  snapshot  =  var.data_snapshot_name_instance1
  physical_block_size_bytes = 4096
}

// Create Log Disk
resource "google_compute_disk" "logdisk_instance1" {
  name  = var.log_disk_name_instance1
  type  = var.disk_type
  size  = var.log_disk_size
  zone  = var.zone1
  snapshot  =  var.log_snapshot_name_instance1
  physical_block_size_bytes = 4096
}

// Create Backup Disk
resource "google_compute_disk" "backupdisk_instance1" {
  name  = var.backup_disk_name_instance1
  type  = var.disk_type
  size  = var.backup_disk_size
  zone  = var.zone1
  snapshot =  var.backup_snapshot_name_instance1
  physical_block_size_bytes = 4096
}

// Attach Data disk
resource "google_compute_attached_disk" "datadiskattach_instance1" {
  disk = google_compute_disk.datadisk_instance1.id
  instance = google_compute_instance.instance1.id
}
// Attach  Log Disk
resource "google_compute_attached_disk" "logdiskattach_instance1" {
  disk = google_compute_disk.logdisk_instance1.id
  instance = google_compute_instance.instance1.id
}

// Attach Backup disk
resource "google_compute_attached_disk" "backupdiskattach_instance1" {
  disk = google_compute_disk.backupdisk_instance1.id
  instance = google_compute_instance.instance1.id
}

The disks are being created from snapshots and the data must be preserved.

How can I attach these disks in the correct order and assign the correct drive letters?

Upvotes: 2

Views: 2514

Answers (1)

miniGweek
miniGweek

Reputation: 470

In Azure we achieve it by running a custom script extension - which basically downloads a powershell script inside the VM and executes it.

I don't know GCP but a quick google search tells me Google Compute lets you setup startup scripts. You could run powershell as a start up script which will do the disk initialization and formatting.

Azure documentation has the powershell documented ( you may need to build on top of this, by adding checks like - are there partitions with type RAW ? etc) https://learn.microsoft.com/en-us/azure/virtual-machines/windows/attach-disk-ps#initialize-the-disk

Terraform docs has a simple example of adding a startup script, you may need to tinker with the syntax to get it up and running with powershell https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance

Upvotes: 2

Related Questions