如何在 terraform 中使用名称获取变量的 id? [英] How to fetch id of a variable using name in terraform?

查看:35
本文介绍了如何在 terraform 中使用名称获取变量的 id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 terraform 资源,我试图在其中使 subnet_id 变量动态化.所以我在下面定义了 subnet_id = worker-subnet-1" 的变量.我想传递子网的名称并获取子网 ID,因为我有多个子网.我该怎么做.

I have a terraform resource in which I am trying to make a subnet_id variable dynamic. So I have varibles defined below in which subnet_id = "worker-subnet-1". I want to pass the name of the subnet and fetch the subnet id as I have multiple subnets. How can I do that.

resource "oci_containerengine_node_pool" "node_pool" {
  for_each       = var.nodepools
  cluster_id     = oci_containerengine_cluster.cluster[0].id
  compartment_id = var.compartment_id
  depends_on     = [oci_containerengine_cluster.cluster]

  kubernetes_version = var.cluster_kubernetes_version
  name               = each.value["name"]

  node_config_details {
    placement_configs {
      availability_domain = var.availability_domain
      subnet_id           = oci_core_subnet.each.value["subnet_name"].id
    }
    size = each.value["size"]
  }

  node_shape = each.value["node_shape"]

  node_shape_config {

    #Optional
    memory_in_gbs = each.value["memory"]
    ocpus         = each.value["ocpus"]
  }

  node_source_details {
    image_id    = each.value["image_id"]
    source_type = "IMAGE"

  }
  ssh_public_key = file(var.ssh_public_key_path)
}

这些是我的变量:

nodepools = {
  np1 = {
    name       = "np1"
    size       = 3
    ocpus      = 8
    memory     = 120
    image_id   = "test"
    node_shape = "VM.Standard2.8"
    subnet_name  = "worker-subnet-1"
  }
  np2 = {
    name       = "np2"
    size       = 2
    ocpus      = 8
    memory     = 120
    image_id   = "test"
    node_shape = "VM.Standard2.8"
    subnet_name  = "worker-subnet-1"
  }
}

有什么建议吗?

resource "oci_core_subnet" "snet-workers" {
  cidr_block                 = lookup(var.subnets["snet-workers"], "subnet_cidr")
  compartment_id             = var.compartment_id
  vcn_id                     = oci_core_virtual_network.base_vcn.id
  display_name               = lookup(var.subnets["snet-workers"], "display_name")
  dns_label                  = lookup(var.subnets["snet-workers"], "dns_label")
  prohibit_public_ip_on_vnic = true
  security_list_ids          = [oci_core_security_list.private_worker_nodes.id]
  route_table_id             = oci_core_route_table.rt-nat.id
}

推荐答案

你必须像下面这样使用 <local resource name> 为你的资源指定的名字

You have to use like below where change <local resource name> to the name you have given for your resource

subnet_id = oci_core_subnet.<local resource name>[each.value.subnet_id].id

这篇关于如何在 terraform 中使用名称获取变量的 id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆