【问题标题】:How to disable a resource in google_compute_instance with Terraform如何使用 Terraform 禁用 google_compute_instance 中的资源
【发布时间】:2019-07-09 08:35:43
【问题描述】:

我想创建一个实例,并根据一些变量创建一个附加磁盘。

...
variable "extra-disk-count" {
    default = "0"
}

variable "extra-disk-size" {
    default = "100"
}

resource "google_compute_instance" "openqa" {
    count        = "${var.count}"
    name         = "${var.name}-${element(random_id.service.*.hex, count.index)}"
    machine_type = "${var.type}"
    zone         = "${var.region}"

    boot_disk {
        initialize_params {
            image = "${var.image_id}"
        }
    }

    attached_disk {
        source      = "${element(google_compute_disk.default.*.self_link, count.index)}"
        device_name = "${element(google_compute_disk.default.*.name, count.index)}"
    }
    ....
}

resource "google_compute_attached_disk" "default" {
  name  = "ssd-disk"
  count = "${var.extra-disk-count}"
  type  = "pd-ssd"
  zone  = "${var.region}"
  size  = "${var.extra-disk-size}"
  physical_block_size_bytes = 4096
}

如果我不想创建磁盘,我可以设置var.extra-disk-count = 0,但是我得到了错误

* google_compute_instance.openqa: element: element() may not be used with an empty list in:

${element(google_compute_disk.default.*.self_link, count.index)}

因为它试图添加一个未创建的元素。

var.extra-disk-count = 0 时,如何“禁用”google_compute_instance 中的attached_disk 选项?

【问题讨论】:

  • 您使用的是哪个版本的 Terraform?
  • Terraform v0.11.13
  • 但如果 0.11 无法做到这一点,如果有更简单的方法,我可以尝试升级到 0.12。
  • 0.12 之前的版本是可能的(取决于您是否依赖google_compute_instance.openqa 资源的输出),但在 0.12 版本中更容易。

标签: google-compute-engine terraform


【解决方案1】:

您可以在 google_compute_instance 中使用“google_compute_attached_disk”代替 attach_disk。 https://www.terraform.io/docs/providers/google/r/compute_attached_disk.html

resource "google_compute_attached_disk" "default" {
  count = "${var.extra-disk-count}"
  disk = "${element(google_compute_disk.openqa.*.self_link, count.index)}"
  instance = "${element(google_compute_instance.default.*.self_link, count.index)}"
}

请注意,extra-disk-count 应为 0 或与 google_compute_attached_disk.default (var.count) 的数量相同。如果 extra-disk-count 大于 var.count,则会出错。如果 extra-disk-count 大于 0 且小于 var.count,则某些计算实例将没有磁盘。

所以我想像下面这样修改,

// remove extra-disk-count

variable "create-extra-disk" {
  default = "false"
}

resource "google_compute_instance" "openqa" {
  count        = "${var.count}"
  name         = "${var.name}-${element(random_id.service.*.hex, count.index)}"
  machine_type = "${var.type}"
  zone         = "${var.region}"

  boot_disk {
    initialize_params {
      image = "${var.image_id}"
    }
  }
}

resource "google_compute_attached_disk" "default" {
  count    =  "${var.create-extra-disk ? var.count: 0}"
  disk     = "${element(google_compute_disk.openqa.*.self_link, count.index)}"
  instance = "${element(google_compute_instance.default.*.self_link, count.index)}"
}

resource "google_compute_disk" "default" {
  name                      = "ssd-disk"
  count                     = "${var.create-extra-disk ? var.count : 0}"
  type                      = "pd-ssd"
  zone                      = "${var.region}"
  size                      = "${var.extra-disk-size}"
  physical_block_size_bytes = 4096
}

如果 create-extra-disk 为 false,则不会创建和附加额外的磁盘。 如果 create-extra-disk 为 true,则会创建额外磁盘并将其附加到所有计算实例。

【讨论】:

  • 效果很好。非常感谢!只是一个小小的修正。最后一个资源应该是“google_compute_disk”
  • 好。我修改为你的评论
猜你喜欢
  • 2021-09-30
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 2019-03-16
  • 2023-02-08
相关资源
最近更新 更多