Reputation: 43
I have the code below in which I create vnets in for_each block:
provider "azurerm" {
features {}
}
variable "vnets" {
type = map(object({
name = string
address_space = list(string)
}))
default = {
"vnet1" = {
"name" = "vnet1",
"address_space" = ["10.0.0.0/16"]
},
"vnet2" = {
"name" = "vnet2",
"address_space" = ["10.1.0.0/16"]
}
}
}
resource "azurerm_resource_group" "vnets" {
name = "vnets"
location = "WestEurope"
}
resource "azurerm_virtual_network" "virtual_network" {
for_each = var.vnets
name = each.value.name
location = "West Europe"
resource_group_name = azurerm_resource_group.vnets.name
address_space = each.value.address_space
}
Everything works with the plan, virtual networks will be created, but the problem is how to get to the created resources from the for_each block?
When I type the command to return the resources list:
terraform state list
Then I have the following output from console:
azurerm_resource_group.vnets
azurerm_virtual_network.virtual_network["vnet1"]
azurerm_virtual_network.virtual_network["vnet2"]
And when I want to use a vnet1 anywhere in the code using reference azurerm_virtual_network.virtual_network["vnet1"] then I'm getting an error.
For example, I want to view resource vnet1:
terraform state show azurerm_virtual_network.virtual_network["vnet1"]
Im getting such error:
Error parsing instance address: azurerm_virtual_network.virtual_network[vnet1]
This command requires that the address references one specific instance.
To view the available instances, use "terraform state list". Please modify
the address to reference a specific instance.
I tried the following commands to access a resource, but they don't work:
terraform state show azurerm_virtual_network.virtual_network["vnet1"]
terraform state show 'azurerm_virtual_network.virtual_network["vnet1"]'
terraform state show azurerm_virtual_network.virtual_network[vnet1]
terraform state show azurerm_virtual_network.virtual_network[0]
terraform state show azurerm_virtual_network.virtual_network.vnet1
Do you know how to solve it?
Upvotes: 4
Views: 9861
Reputation: 81
In addition, if you want to show a specific instance in the state file, you can use terraform state show 'azurerm_virtual_network.virtual_network[\"vnet1\"]'
I ran into this same issue with using modules. The quoted above is the correct answer and the following is how to perform it with a module:
terraform state show 'module.module_name["module_instance"].resource.resource_name["resource_instance"]
Example:
state show 'module.rsm_keyvault_solution_module_resource["development"].azurerm_key_vault.keyvault_module_resource["rsm-playground-dev"]'
Upvotes: 8
Reputation: 28284
In this case, you can use the values function to take a map and return a list containing the values of the elements in that map.
For example, to get the values of VNets in a map:
output "azurerm_vnets_names" {
value = values(azurerm_virtual_network.virtual_network)[*].name
}
Or get a specific VNet name like this:
output "azurerm_vnet1_name" {
value = values(azurerm_virtual_network.virtual_network)[0].name
}
In addition, if you want to show a specific instance in the state file, you can use
terraform state show 'azurerm_virtual_network.virtual_network[\"vnet1\"]'
Upvotes: 2