【问题标题】:How can I skip optional arguments in a terraform resource based on a condition如何根据条件跳过 terraform 资源中的可选参数
【发布时间】:2020-09-05 02:53:09
【问题描述】:

我正在使用 Terraform 模块来维护多个环境,对于模块中的其中一个资源,有一个可选参数,我只想用于一个环境而不是其余环境。

例如,在下面的资源中,我只希望参数node_shape_config 应用于一个环境,而不是其他环境。有没有办法在 terraform 中做到这一点?

resource "oci_containerengine_node_pool" "nodepools" {
  cluster_id     = oci_containerengine_cluster.k8s_cluster.id
  compartment_id = var.oci_vcn.compartment_id
  depends_on     = ["oci_containerengine_cluster.k8s_cluster"]

  kubernetes_version = var.oke_cluster.kubernetes_version
  name               = "${var.oci_vcn.label_prefix}-np-${count.index + 1}"
  subnet_ids         = [var.oke_cluster.worker_subnet_id_1, var.oke_cluster.worker_subnet_id_2, var.oke_cluster.worker_subnet_id_3]

  node_shape_config {
    #Optional
    ocpus = 4
  }

  node_source_details {
    image_id      = var.node_pools.create_custom_image == true ? var.node_pools.custom_image_id : var.node_pools.node_pool_node_image_id
    source_type   = "IMAGE"
  }

  ...
  ...
  ...

  count = var.node_pools.node_pool_count
}

【问题讨论】:

    标签: terraform terraform0.12+


    【解决方案1】:

    您正在寻找的是dynamic blocks。您需要使 node_shape_config 块动态,您可以这样做:

    # assuming you have variable for environment
    dynamic "node_shape_config" {
        for_each = var.environment === "development" ? toset([1]) : toset([])
        content {
          ocpus = 4
        }
      }
    

    它只会为development 环境设置node_shape_config

    【讨论】:

    • 非常感谢,我不知道动态块。我今天学了些新东西。我会验证这是否适合我。
    猜你喜欢
    • 2021-01-12
    • 2022-08-18
    • 2021-12-22
    • 2017-12-09
    • 2021-12-21
    • 2018-01-21
    • 2020-12-26
    • 2021-10-14
    • 2020-04-22
    相关资源
    最近更新 更多