I created 3 subnets using the for_each function and I outputted some values. However the values are being outputted altogether as a map and I need them to be outputted seperately so I can use them as inputs for other resources
my root module 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
}
My variables.tf in my root module
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"
}
}
}
My root module output.tf
output "subnet_name" {
value = {
for k, subnet in module.deploy_subnet : k => subnet.subnet_name
}
}
output "subnet_id" {
value = {
for k, subnet in module.deploy_subnet : k => subnet.subnet_id
}
}
output "subnet_cidr_block" {
value = {
for k, subnet in module.deploy_subnet : k => subnet.subnet_cidr_block
}
}
My child module 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
}
Child module output.tf
output "subnet_name" {
value = azurerm_subnet.obc_subnet.name
}
output "subnet_id" {
value = azurerm_subnet.obc_subnet.id
}
output "subnet_cidr_block" {
value = azurerm_subnet.obc_subnet.address_prefixes
}
I am getting the output like this for example
subnet_name = {
"sub-1" = "aks-sn"
"sub-2" = "postgres-sn"
"sub-3" = "keyvault-sn"
}
I want to get them to output their values individually so I can use them elsewhere to create resources