简体   繁体   English

如何将数据源中for_each循环的key传递给资源配置?

[英]How to pass key of for_each loop in a data source to the resource configuration?

I have been wanting to create a " azurerm_virtual.network " by passing location and name of the resource group attribute using data source: " azurerm_resource_group ".我一直想通过使用数据源传递资源组属性的位置和名称来创建一个“ azurerm_virtual.network ”:“ azurerm_resource_group ”。 My data source itself is created using " for_each ".我的数据源本身是使用“ for_each ”创建的。 The idea here is, if I want to provision more .NETs in different groups, I just add more resource group names into the variable list, " var.network_rg ".这里的想法是,如果我想在不同的组中提供更多的 .NET,我只需将更多的资源组名称添加到变量列表“ var.network_rg ”中。 However, I am not able to figure out how to pass this list of values as an input to the attributes of .net resource which is also created using for_each.但是,我无法弄清楚如何将此值列表作为输入传递给同样使用 for_each 创建的 .net 资源的属性。 The loop seems to be taking the value from resource config " for_each " instead of data source.该循环似乎是从资源配置“ for_each ”而不是数据源中获取值。 Can anyone please help identify what is wrong here?任何人都可以帮助确定这里出了什么问题吗? Is this even the correct way to achieve what I'm trying to do?这甚至是实现我想要做的事情的正确方法吗? Below is my code:下面是我的代码:

child main.tf子main.tf

data "azurerm_resource_group" "network_rg" {
  for_each = toset(var.network_rg)
  name     = "${var.owner}_${var.env}_${each.key}_${var.location_short}"
}

resource "azurerm_virtual_network" "vnet" {
  for_each            = var.vnet
  name                = "${var.owner}_${var.env}_${each.value["name"]}_${var.location_short}}"
  location            = data.azurerm_resource_group.network_rg[each.key].location
  resource_group_name = data.azurerm_resource_group.network_rg[each.key].name
  address_space       = each.value["address_space"]
  lifecycle {
    ignore_changes = [
      tags["Created"]
    ]
  }
  tags = {
    "Purpose" = var.purpose_tag #"Test"
    }
}

child variable.tf子变量.tf

variable "owner" {
  type        = string
  description = "Owner of the resource"
}
variable "network_rg" {
  type        = list(string)
  description = "RG in which VNET is to be created"
}
variable "location" {
  type        = string
  description = "Location in which resource group needs to be created"
}
variable "env" {
  type        = string
  description = "Environment to be deployed in"
}
variable "location_short" {
  type        = string
  description = "Short name for location"
}
variable "vnet" {
  type = map
  description = "Map of VNET attributes"
}
variable "purpose_tag" {
  type        = string
  description = "Purpose Tag"
}

.tfvars .tfvars

owner = "rrb"
location = "australiaeast"
env = "dev"
location_short = "aue"
purpose_tag = "Test"
####################### Resource Group Module ####################################
rg_name = ["platform"] #add rg name here to create more rg

###################### Network Module ############################################

network_rg = ["platform"]
vnet = {
    hub_vnet = {
        name = "hub"
        address_space = ["10.200.0.0/16"]
    }
    spoke_vnet = {
        name = "spoke"
        address_space = ["10.201.0.0/16"]
    }
}

Root main.tf根main.tf

module "rg" {
    source = "./modules/resourceGroup"
    owner = var.owner
    env  = var.env
    rg_name = var.rg_name
    location = var.location
    location_short = var.location_short
    purpose_tag = var.purpose_tag
}

module "network" {
    source = "./modules/network"
    network_rg = var.network_rg
    vnet = var.vnet
    owner = var.owner
    env  = var.env
    location = var.location
    location_short = var.location_short
    purpose_tag = var.purpose_tag
}

Error Message错误信息

│ Error: Invalid index
│ 
│   on modules/network/main.tf line 10, in resource "azurerm_virtual_network" "vnet":
│   10:   location            = data.azurerm_resource_group.network_rg[each.key].location
│     ├────────────────
│     │ data.azurerm_resource_group.network_rg is object with 1 attribute "platform"
│     │ each.key is "spoke_vnet"
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on modules/network/main.tf line 10, in resource "azurerm_virtual_network" "vnet":
│   10:   location            = data.azurerm_resource_group.network_rg[each.key].location
│     ├────────────────
│     │ data.azurerm_resource_group.network_rg is object with 1 attribute "platform"
│     │ each.key is "hub_vnet"
│ 
│ The given key does not identify an element in this collection value.

Output of for_each loop as per below suggestion按照以下建议,for_each 循环的 Output

> {for idx, val in setproduct(keys(var.vnet), var.network_rg): idx => val}
{
  "0" = [
    "hub_vnet",
    "platform",
  ]
  "1" = [
    "spoke_vnet",
    "platform",
  ]
}

You have to iterate over vents and.network_rgs.您必须遍历 vents 和 .network_rgs。 Normally this would be done using double for loop, but in you case you could use setproduct as well.通常这将使用双循环来完成,但在您的情况下,您也可以使用setproduct

resource "azurerm_virtual_network" "vnet" {
  for_each            = {for idx, val in setproduct(keys(var.vnet), var.network_rg): idx => val}
  name                = "${var.owner}_${var.env}_${var.vnet[each.value[0]].name}_${var.location_short}}"

  location            = data.azurerm_resource_group.network_rg[each.value[1]].location

  resource_group_name = data.azurerm_resource_group.network_rg[each.value[1]].name

  address_space       = var.vnet[each.value[0]].address_space
  lifecycle {
    ignore_changes = [
      tags["Created"]
    ]
  }
  tags = {
    "Purpose" = var.purpose_tag #"Test"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM