【问题标题】:using Terraform, how to increase root disk of GCP instance without destroying the instance使用 Terraform,如何在不破坏实例的情况下增加 GCP 实例的根磁盘
【发布时间】:2023-01-27 01:39:44
【问题描述】:

我有一个使用 Terraform 创建的 GCP 实例。当我增加其根磁盘的大小时,Terraform 会尝试销毁并重新创建一个不可接受的新实例。这是我的地形代码:

resource "google_compute_instance" "test" {
  ...
  boot_disk {
  auto_delete = true
    initialize_params {
      image = var.image
      size  = 10 # I want to change it to 20
      type  = "pd-standard"
    }
  }
  ...
}

var.image 是:https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20201028

如何在不重新创建 gcp 实例的情况下调整根磁盘的大小? (我想我可以通过手动调整大小来避免重新创建实例吗?请参阅How can size of the root disk in Google Compute Engine be increased?。但我不想手动调整大小,因为我需要调整很多实例。此外,如果我手动更改大小,terraform将显示漂移)。

请注意 AWS EC2,当我们更改大小时,terraform 不会重新创建新的 EC2。

【问题讨论】:

    标签: google-cloud-platform terraform-provider-gcp gcp-compute-instance


    【解决方案1】:

    来自https://github.com/hashicorp/terraform-provider-google/issues/12124

    我理解“initialize_params 表示“我希望使用这些属性创建实例”意味着在更改时重新创建它是提供者的预期行为”

    来自https://github.com/hashicorp/terraform-provider-google/issues/6087#issuecomment-619270971

    我理解“您可以在 Terraform 中创建一个可以随时更新的磁盘,我们可以让 initialize_params 成为真正意味着它所说的东西:初始化磁盘时设置的参数”

    以下是如何制作可以在 terraform 中调整大小的磁盘:

    data "google_compute_image" "my_image" {
      family  = "debian-9"
      project = "debian-cloud"
    }
    
    resource "google_compute_disk" "foobar" {
      name  = "my-disk"
      zone  = "us-central1-a"
      // only use an image data source if you're ok with the disk recreating itself with a new image periodically
      image = data.google_compute_image.my_image.self_link
    }
    
    resource "google_compute_instance" "foobar" {
      name         = "my-instance"
      machine_type = "n1-standard-1"
      zone         = "us-central1-a"
    
      boot_disk {
        source = google_compute_disk.foobar.name
      }
    
      network_interface {
        network = "default"
      }
    }
    

    这样,您将能够使用 terraform 动态更改磁盘规格,而不必使用用于重新创建而非修改的 initialize_params。

    https://github.com/hashicorp/terraform-provider-google/issues/12655 和此答案中的先前链接,看起来 terraform 不允许您更改 initialize_params 的工作方式:这些是为了告诉您需要一个实例创建, 未修改。

    “initialize_params 是一个只能创建的字段。更新此字段中的任何内容都被定义为重新创建/销毁行为。如果您想进行就地更新,我相信上面评论中指出的源字段将是你需要什么。”

    【讨论】:

      猜你喜欢
      • 2017-08-13
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 2021-08-28
      • 2014-08-21
      • 2021-12-12
      • 2017-10-15
      相关资源
      最近更新 更多