使用 for_each 循环创建的多个子网的值输出
Output of values from multiple subnets created using a for_each loop
我使用如下所示的 for_each 循环创建了一些子网
main.tf 在子模块中
resource "azurerm_subnet" "obc_subnet" {
name = var.subnet_name
address_prefixes = var.address_prefixes
resource_group_name = var.resource_group_name
virtual_network_name = var.vnet_name
}
main.tf 根模块
module "deploy_subnet" {
source = "./modules/azure_subnet"
for_each = var.prefix
subnet_name = each.value["name"]
address_prefixes = [each.value["cidr"]]
subscription_id = var.subscription_id
resource_group_name = var.resource_group_name
region = var.region
vnet_name = var.vnet_name
}
variables.tf 根模块
variable "prefix" {
type = map(object({
name = string
cidr = string
}))
default = {
sub-1 = {
name = "aks-sn"
cidr = "10.0.1.0/24"
}
sub-2 = {
name = "postgres-sn"
cidr = "10.0.2.0/24"
}
sub-3 = {
name = "keyvault-sn"
cidr = "10.0.3.0/24"
}
}
}
一切正常,除非我尝试输出与 3 个子网关联的值(例如 id)
我正在尝试在输出中使用 for k 语法,但我不太了解它的外观。
我的子模块output.tf中有以下内容
output "subnet_ids" {
value = tomap({
for k, subnets in azurerm_subnet.obc_subnet : k => subnets.id
})
}
我不确定如何处理根模块中的 output.tf,所以我将其保留原样
output "subnet_id" {
value = module.deploy_subnet.subnet_id
}
我收到一条错误消息,指出该值没有属性
Error:
Unsupported attribute
on modules/azure_subnet/output.tf line 3, in output "subnets_id":
3: for k, subnets in azurerm_subnet.obc_subnet : k => subnets.is
This value does not have any attributes.
接近,但相反。
子模块中的azurerm_subnet
资源不是一个集合,所以那里的输出应该是:
output "subnet_id" {
value = azurerm_subnet.obc_subnet.id
}
但是在根模块中您有子模块的集合。例如:
output "subnet_ids" {
value = {
for k, subnet in module.deploy_subnet : k => subnet.subnet_id
}
}
我使用如下所示的 for_each 循环创建了一些子网
main.tf 在子模块中
resource "azurerm_subnet" "obc_subnet" {
name = var.subnet_name
address_prefixes = var.address_prefixes
resource_group_name = var.resource_group_name
virtual_network_name = var.vnet_name
}
main.tf 根模块
module "deploy_subnet" {
source = "./modules/azure_subnet"
for_each = var.prefix
subnet_name = each.value["name"]
address_prefixes = [each.value["cidr"]]
subscription_id = var.subscription_id
resource_group_name = var.resource_group_name
region = var.region
vnet_name = var.vnet_name
}
variables.tf 根模块
variable "prefix" {
type = map(object({
name = string
cidr = string
}))
default = {
sub-1 = {
name = "aks-sn"
cidr = "10.0.1.0/24"
}
sub-2 = {
name = "postgres-sn"
cidr = "10.0.2.0/24"
}
sub-3 = {
name = "keyvault-sn"
cidr = "10.0.3.0/24"
}
}
}
一切正常,除非我尝试输出与 3 个子网关联的值(例如 id)
我正在尝试在输出中使用 for k 语法,但我不太了解它的外观。
我的子模块output.tf中有以下内容
output "subnet_ids" {
value = tomap({
for k, subnets in azurerm_subnet.obc_subnet : k => subnets.id
})
}
我不确定如何处理根模块中的 output.tf,所以我将其保留原样
output "subnet_id" {
value = module.deploy_subnet.subnet_id
}
我收到一条错误消息,指出该值没有属性
Error:
Unsupported attribute
on modules/azure_subnet/output.tf line 3, in output "subnets_id":
3: for k, subnets in azurerm_subnet.obc_subnet : k => subnets.is
This value does not have any attributes.
接近,但相反。
子模块中的azurerm_subnet
资源不是一个集合,所以那里的输出应该是:
output "subnet_id" {
value = azurerm_subnet.obc_subnet.id
}
但是在根模块中您有子模块的集合。例如:
output "subnet_ids" {
value = {
for k, subnet in module.deploy_subnet : k => subnet.subnet_id
}
}