简体   繁体   中英

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?

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\"]'

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\"]'

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM