gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:06:29 25 4
gpt4 key购买 nike

我一直想通过使用数据源“azurerm_resource_group”传递资源组属性的位置和名称来创建“azurerm_virtual_network”。我的数据源本身是使用“for_each”创建的。这里的想法是,如果我想在不同的组中配置更多的 VNET,我只需将更多的资源组名称添加到变量列表“var.network_rg”中。但是,我无法弄清楚如何将此值列表作为输入传递给也是使用 for_each 创建的 vnet 资源的属性。该循环似乎从资源配置“for_each”而不是数据源中获取值。任何人都可以帮助确定这里出了什么问题吗?这是否是实现我想要做的事情的正确方法?下面是我的代码:

子 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"
}
}

子变量.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

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"]
}
}

根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: 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.

按照以下建议输出 for_each 循环

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

最佳答案

您必须迭代 vents 和 network_rgs。通常,这可以使用双 for 循环来完成,但在您的情况下,您可以使用 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"
}
}

关于azure - 如何将数据源中for_each循环的key传递给资源配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74759739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com