Google-Cloud-Platform

使用 Terraform 創建 GCE 實例,附加輔助磁碟時出錯?

  • April 18, 2016

我的 terraform 文件看起來像這樣。

resource "google_compute_disk" "sqlserversecondary" {
   name = "sql-server-secondary"
   type = "pd-ssd"
   zone = "us-central1-a"
   size = "512"
}

resource "google_compute_instance" "sqlserver2016rc2" {
   name = "sqlserver2016rc2"
   machine_type = "n1-highmem-8"

   tags = ["database", "sqlserver2016rc2"]
   zone = "us-central1-a"

   disk {
       image = "sqlserver2016"
       size = "120"
   }

   disk {
       image = "sql-server-secondary"
   }

   network_interface {
       network = "default"
       access_config {
           // Ephemeral IP
       }
   }

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

第二個磁碟存在於 GCE 中,但在這種特殊情況下,我不斷收到以下錯誤。

Error applying plan:

1 error(s) occurred:

* google_compute_instance.sqlserver2016rc2: Error creating instance: googleapi: Error 400: Invalid value for field 'resource.disks[1].initializeParams': ''. Cannot specify initializeParams for non-boot persistent disks., invalid

對於您的第二個磁碟,您希望使用disk = "sql-server-secondary"而不是image作為標識符。此外,最好使用 Terraform 的插值語法(例如${...})引用第二個磁碟,以確保以適當的順序創建和管理您的資源。

resource "google_compute_disk" "sqlserversecondary" {
 name = "sql-server-secondary"
 type = "pd-ssd"
 zone = "us-central1-a"
 size = "512"
}

resource "google_compute_instance" "sqlserver2016rc2" {
 name         = "sqlserver2016rc2"
 machine_type = "n1-highmem-8"

 tags = ["database", "sqlserver2016rc2"]
 zone = "us-central1-a"

 disk {
   image = "ubuntu-1404-trusty-v20160114e"
   size  = "120"
 }

 disk {
   disk = "${google_compute_disk.sqlserversecondary.name}" // s/image/disk
 }

 network_interface {
   network = "default"

   access_config {
     // Ephemeral IP
   }
 }

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

InitializeParams來自預先存在的圖像(程式碼)的磁碟的Terraform 集會導致您收到錯誤。在這種情況下似乎可能存在錯誤,從現有映像附加非啟動磁碟,但我仍在深入研究。

希望這可以幫助!

引用自:https://serverfault.com/questions/771239