【问题标题】:GCP Terraform Error waiting for disk to attach: The disk resource disk resource is already in use等待磁盘附加的 GCP Terraform 错误:磁盘资源磁盘资源已在使用中
【发布时间】:2021-06-19 11:55:13
【问题描述】:

我能够使用 terraform 在 GCP 中创建实例和卷。但是当我对卷进行快照时,我得到一个错误并且磁盘不会附加:

Error: Error waiting for disk to attach: The disk resource 'projects/btgcp-iaas-dev/zones/us-east1-d/disks/dev-sql-td-data-disk' is already being used by 'projects/btgcp-iaas-dev/global/snapshots/dev-sql-td-data-disk-volume-snapshot'


  on main.tf line 83, in resource "google_compute_attached_disk" "datadiskattach":
  83: resource "google_compute_attached_disk" "datadiskattach" {

这就是我定义磁盘、附加磁盘和快照资源的方式:

// Create additional disks
resource "google_compute_disk" "datadisk" {
  name  = var.data_disk_name
  type  = var.data_disk_type
  size  = var.data_disk_size
  zone  = var.zone
  image = data.google_compute_image.sqlserverimage.self_link
  labels = {
    environment = "dev"
    asv = "mycompanytools"
    ownercontact = "myuser"
  }
  physical_block_size_bytes = 4096
}

// Attach additional disks
resource "google_compute_attached_disk" "datadiskattach" {
  disk = google_compute_disk.datadisk.id
  instance = google_compute_instance.default.id
}

// Create a snapshot of the datadisk volume
resource "google_compute_snapshot" "datadisksnapshot" {
  name        = var.datadisk_volume_snapshot_name
  source_disk = google_compute_disk.datadisk.name
  zone        = var.zone
  labels = {
    my_label = var.datadisk_volume_snapshot_label
  }
}

这是我在变量中命名它们的方式。命名似乎与我得到的错误发生冲突:

// Create Data Disk Variables
variable "data_disk_name" {
  description = "Value of the disk name for the data disk for the GCP instance"
  type        = string
  default     = "dev-sql-td-data-disk"
}

// Create Snapshot Variables
variable "datadisk_volume_snapshot_name" {
  description = "Value of the datadisk name for the GCP instance root disk volume"
  type        = string
  default     = "dev-sql-td-data-disk-volume-snapshot"
}

我怎样才能通过这个错误?

【问题讨论】:

    标签: terraform terraform-provider-gcp


    【解决方案1】:

    Terraform 文档建议,“使用 google_compute_attached_disk 时,您必须在已附加磁盘的 google_compute_instance 资源上使用生命周期.ignore_changes = ["attached_disk"]。否则这两个资源将争夺对附加磁盘块的控制权” 参考链接:https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_attached_disk

    【讨论】:

      【解决方案2】:

      将此添加到您的“google_compute_instance”中

      lifecycle { 
          ignore_changes = [
            attached_disk,
            ] 
        }
      

      more information

      【讨论】:

      • 嗨@Steve 我已经在下面提供了更多信息的链接
      【解决方案3】:

      此外,您可以删除以下内容,因为Terraform 会告诉您资源已被使用。

      // Attach additional disks
      resource "google_compute_attached_disk" "datadiskattach" {
          disk = google_compute_disk.datadisk.id
          instance = google_compute_instance.default.id
      }
      

      【讨论】:

      • edit您的帖子使用正确的代码格式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 2017-09-13
      • 2018-03-09
      • 2022-01-20
      • 2015-07-12
      • 2017-07-12
      相关资源
      最近更新 更多