中获取子网值时出现

中获取子网值时出现

本文介绍了在 oci 中获取子网值时出现 Terraform 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,我试图通过子网名称获取子网的 ID:

I have this code where I am trying to fetch the id of subnet via subnet name:

代码如下所示:

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.snet-workers[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)
}

我的子网代码如下:

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
}

变量看起来像:

variable "subnets" {
  description = "List of subnets to create for an environment"
  type = map(object({
    subnet_cidr  = string
    display_name = string
    dns_label    = string
  }))
}

我的 tfvar 看起来像

my tfvars looks like

nodepools = {
  np1 = {
    name       = "np1"
    size       = 3
    ocpus      = 8
    memory     = 120
    image_id   = "test"
    node_shape = "VM.Standard2.8"
    subnet_name = "snet-worker1"
  }
  np2 = {
    name       = "np2"
    size       = 2
    ocpus      = 8
    memory     = 120
    image_id   = "test"
    node_shape = "VM.Standard2.8"
    subnet_name = "snet-worker2"
  }
}

但在 terraform 计划中我遇到了错误

But in terraform plan I am getting error as

 Error: Invalid index
│
│   on ../modules/oke/oke.tf line 39, in resource "oci_containerengine_node_pool" "node_pool":
│   39:       subnet_id           = oci_core_subnet.snet-workers[each.value.subnet_name].id
│     ├────────────────
│     │ each.value.subnet_name is "snet-worker2"
│     │ oci_core_subnet.snet-workers is object with 22 attributes
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│   on ../modules/oke/oke.tf line 39, in resource "oci_containerengine_node_pool" "node_pool":
│   39:       subnet_id           = oci_core_subnet.snet-workers[each.value.subnet_name].id
│     ├────────────────
│     │ each.value.subnet_name is "snet-worker1"
│     │ oci_core_subnet.snet-workers is object with 22 attributes
│
│ The given key does not identify an element in this collection value.

谁能帮忙

推荐答案

如下:

subnet_id           =  oci_core_subnet.snet-workers[each.value.subnet_name].id

只有在创建 oci_core_subnet.snet-workers 时使用了 for_each 才能工作.由于您没有使用 for_each,因此应该是:

would only work if you had used for_each while creating oci_core_subnet.snet-workers. Since you are not using for_each, it should be:

subnet_id           =  oci_core_subnet.snet-workers.id

更新

继续使用原始版本:

resource "oci_core_subnet" "snet-workers" {

  for_each                   = var.subnets

  cidr_block                 = each.value["subnet_cidr"]
  compartment_id             = var.compartment_id
  vcn_id                     = oci_core_virtual_network.base_vcn.id
  display_name               = leach.value[""display_name"]
  dns_label                  = each.value["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
}

这篇关于在 oci 中获取子网值时出现 Terraform 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:49