【问题标题】:Terraform: If subnetwork does not exist, set interface to nullTerraform:如果子网不存在,则将 interface 设置为 null
【发布时间】:2019-09-29 06:24:04
【问题描述】:

我正在创建一个在 GCP 中创建多网卡 VM 的自定义模块。在部署时,可能会出现 VM 不需要使用多个接口的情况。

如果子网不存在,是否可以将 network_interface 设置为空值?如果可能,我想避免为每个接口数创建多个模块。


resource "google_compute_instance" "vm" {
  name                      = "${var.vm_name}"
  machine_type              = "${var.machine_type}"
  zone                      = "${var.zone}"
  min_cpu_platform          = "${var.cpu_platform}"

  network_interface {
    subnetwork    = "${google_compute_subnetwork.subnetwork1.name}"
  }
  network_interface {
    subnetwork    = "${google_compute_subnetwork.subnetwork2.name}"
  }

  network_interface {

// PSEUDO CODE
    subnetwork   = if (subnetwork3 == true) {
                      "${google_compute_subnetwork.subnetwork3.name}"
                   else 
                      "do nothing or set null"
  }
}

【问题讨论】:

  • 将参数设置为 null 在 0.12 中可用。

标签: terraform terraform-provider-gcp


【解决方案1】:

您可以将localscount 结合起来吗?

例子

locals {
  interface_num = "${var.is_subnetwork_3 ? 0 : 3}"
}

resource "google_whatever" "name" {
  count = "${local.interface_num}"
  // config
}

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2022-01-06
    相关资源
    最近更新 更多