Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - How to get the reference of resources created in for_each block

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?

like image 805
criss Avatar asked Oct 15 '25 18:10

criss


1 Answers

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"]'
like image 171
Craig Ramsey Avatar answered Oct 18 '25 19:10

Craig Ramsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!