gpt4 book ai didi

azure - 使用 for_each 资源 block 中的资源 ID

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

我创建了一个 Terraform 模板,该模板使用 for_each 命令创建 2 个路由表和 2 个子网。我正在尝试将路由表关联到两个子网,但是我很难这样做,因为我不知道如何获取路由表和子网的 ID,因为详细信息不在变量中,而且我不确定如何获取该信息并使用它。请问有人可以提供帮助吗?

谢谢

主模板

# SUBNETS DEPLOYMENT
resource "azurerm_subnet" "subnets" {
depends_on = [azurerm_virtual_network.vnet]
for_each = var.subnets
resource_group_name = var.rg.name
virtual_network_name = var.vnet.config.name
name = each.value.subnet_name
address_prefixes = each.value.address_prefixes
}

# ROUTE TABLE DEPLOYMENT
resource "azurerm_route_table" "rt" {
depends_on = [azurerm_virtual_network.vnet]
for_each = var.rt
name = each.value.route_table_name
resource_group_name = var.rg.name
location = var.rg.location
disable_bgp_route_propagation = true
route = [ {
address_prefix = each.value.address_prefix
name = each.value.route_name
next_hop_in_ip_address = each.value.next_hop_ip
next_hop_type = each.value.next_hop_type
} ]
}

# ROUTE TABLE ASSOICATION
resource "azurerm_subnet_route_table_association" "rt_assoication" {
subnet_id = azurerm_subnet.subnets.id
route_table_id = azurerm_route_table.rt.id
}

变量

# SUBNET VARIBALES
variable "subnets" {
description = "subnet names and address prefixes"
type = map(any)
default = {
subnet1 = {
subnet_name = "snet-001"
address_prefixes = ["172.17.208.0/28"]
}
subnet2 = {
subnet_name = "snet-002"
address_prefixes = ["172.17.208.32/27"]
}
}
}

# ROUTE TABLES VARIABLES
variable "rt" {
description = "variable for route tables."
type = map(any)
default = {
rt1 = {
route_table_name = "rt1"
address_prefix = "0.0.0.0/0"
route_name = "udr-azure-firewall"
next_hop_ip = "10.0.0.0"
next_hop_type = "VirtualAppliance"
}
rt2 = {
route_table_name = "rt2"
address_prefix = "0.0.0.0/0"
route_name = "udr-azure-firewall"
next_hop_ip = "10.0.0.0"
next_hop_type = "VirtualAppliance"
}
}
}

运行 terraform plan 时出现的错误是:

│ Error: Missing resource instance key

│ on modules\vnet\main.tf line 74, in resource "azurerm_subnet_route_table_association" "rt_assoication":
│ 74: subnet_id = azurerm_subnet.subnets.id

│ Because azurerm_subnet.subnets has "for_each" set, its attributes must be accessed on specific instances.

│ For example, to correlate with indices of a referring resource, use:
│ azurerm_subnet.subnets[each.key]


│ Error: Missing resource instance key

│ on modules\vnet\main.tf line 75, in resource "azurerm_subnet_route_table_association" "rt_assoication":
│ 75: route_table_id = azurerm_route_table.rt.id

│ Because azurerm_route_table.rt has "for_each" set, its attributes must be accessed on specific instances.

│ For example, to correlate with indices of a referring resource, use:
│ azurerm_route_table.rt[each.key]

最佳答案

看起来您已经差不多完成了,在子网路由表关联 block 中更新以下内容,它应该可以工作:

# ROUTE TABLE ASSOICATION
resource "azurerm_subnet_route_table_association" "rt_assoication" {
subnet_id = azurerm_subnet.subnets[each.key].id
route_table_id = azurerm_route_table.rt[each.key].id
}

关于azure - 使用 for_each 资源 block 中的资源 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73699279/

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