遍历 terraform 模块中的对象映射
Iterating through map of objects in terraform modules
我需要构建2-3个vnet,在单个vnet下,我们需要构建2个子网。
下面是我的代码:
**variables.tf**
variable "vnet_config" {
type = map(object({
vnet_name = string
address_space = list(string)
}))
}
**vnet.auto.tfvars**
vnet_config = {
vnet1 = {
vnetname = "bupa1"
address_space = ["10.0.0.0/16"]
},
vnet2 = {
vnetname = "bupa2"
address_space = ["192.168.0.0/24"]
}
}
****main.tf****
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.98.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg1" {
name = "rg1"
location = "West Europe"
}
module vnet {
source = "./vnet"
resource_group_name = azurerm_resource_group.rg1.name
for_each = var.vnet_config
vnet_name = each.value.vnetname
address_space = each.value.address_space
}
module "subnet" {
source = "./subnet"
resource_group_name = azurerm_resource_group.example.name
vnet_name = module.vnet.vnet_name
subnet_name = "test20"
subnet_address_prefixes = "10.0.1.0/24"
}
output.tf
output "vnet_id" {
description = "The id of the newly created vNet"
value = azurerm_virtual_network.vnet.id
}
output "vnet_name" {
description = "The Name of the newly created vNet"
value = azurerm_virtual_network.vnet.name
}
output "vnet_location" {
description = "The location of the newly created vNet"
value = azurerm_virtual_network.vnet.location
}
output "vnet_address_space" {
description = "The address space of the newly created vNet"
value = azurerm_virtual_network.vnet.address_space
}
vnet_name = module.vnet.vnet_name
以上语句将不起作用,因为在 vnet 模块中创建了两个 vnet。
我已经创建了 vnet 和子网的模块,我正在调用它们来创建 vnet 和子网。
请你帮我解决这个问题。
谢谢
您可以为每个子网创建一个唯一的引用。对于每一个,您都可以创建多个副本,每个 vnet 一个。
module "subnet1" {
for_each = module.vnet
...
vnet_name = each.value.vnet_name
subnet_name = "test10"
}
module "subnet2" {
for_each = module.vnet
...
vnet_name = each.value.vnet_name
subnet_name = "test20"
}
既然你使用了for_each,如果你想引用单个vnet的使用,
module."NAME"["KEY"].ATTRIBUTE 例如
module.vnet["vnet1"].vnet_name
我需要构建2-3个vnet,在单个vnet下,我们需要构建2个子网。
下面是我的代码:
**variables.tf**
variable "vnet_config" {
type = map(object({
vnet_name = string
address_space = list(string)
}))
}
**vnet.auto.tfvars**
vnet_config = {
vnet1 = {
vnetname = "bupa1"
address_space = ["10.0.0.0/16"]
},
vnet2 = {
vnetname = "bupa2"
address_space = ["192.168.0.0/24"]
}
}
****main.tf****
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.98.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg1" {
name = "rg1"
location = "West Europe"
}
module vnet {
source = "./vnet"
resource_group_name = azurerm_resource_group.rg1.name
for_each = var.vnet_config
vnet_name = each.value.vnetname
address_space = each.value.address_space
}
module "subnet" {
source = "./subnet"
resource_group_name = azurerm_resource_group.example.name
vnet_name = module.vnet.vnet_name
subnet_name = "test20"
subnet_address_prefixes = "10.0.1.0/24"
}
output.tf
output "vnet_id" {
description = "The id of the newly created vNet"
value = azurerm_virtual_network.vnet.id
}
output "vnet_name" {
description = "The Name of the newly created vNet"
value = azurerm_virtual_network.vnet.name
}
output "vnet_location" {
description = "The location of the newly created vNet"
value = azurerm_virtual_network.vnet.location
}
output "vnet_address_space" {
description = "The address space of the newly created vNet"
value = azurerm_virtual_network.vnet.address_space
}
vnet_name = module.vnet.vnet_name
以上语句将不起作用,因为在 vnet 模块中创建了两个 vnet。
我已经创建了 vnet 和子网的模块,我正在调用它们来创建 vnet 和子网。
请你帮我解决这个问题。
谢谢
您可以为每个子网创建一个唯一的引用。对于每一个,您都可以创建多个副本,每个 vnet 一个。
module "subnet1" {
for_each = module.vnet
...
vnet_name = each.value.vnet_name
subnet_name = "test10"
}
module "subnet2" {
for_each = module.vnet
...
vnet_name = each.value.vnet_name
subnet_name = "test20"
}
既然你使用了for_each,如果你想引用单个vnet的使用, module."NAME"["KEY"].ATTRIBUTE 例如 module.vnet["vnet1"].vnet_name