Referencing outputs from a for_each module

I have two modules in which resources are created using for_each.

I want to use output from one module as an input to the second module.

Here are the modules:

NIC module:

resource "azurerm_network_interface" "nic" { for_each = var.nics name = each.value["nic_name"] location = each.value["location"] resource_group_name = each.value["rg_name"] ip_configuration { name = each.value["ipconfig-name"] subnet_id = each.value["subnet-id"] private_ip_address_allocation = "Dynamic" } } variable "nics" { type = map(object( { nic_name = string location = string rg_name = string ipconfig-name = string subnet-id = string } )) } output "nic_ids" { value = [for x in azurerm_network_interface.nic : x.id] } output "map_nic_ids" { value = { for x in azurerm_network_interface.nic : x.name => x.id } } 

VM module:

resource "azurerm_linux_virtual_machine" "vm" { for_each = var.vms name = each.value["vm_name"] location = each.value["location"] resource_group_name = each.value["rg_name"] admin_username = each.value["admin_username"] admin_password = var.admin_password network_interface_ids = var.nic_ids disable_password_authentication = false size = "Standard_B1s" os_disk { caching = "ReadOnly" storage_account_type = "Standard_LRS" name = each.value["os_disk_name"] disk_size_gb = "40" } source_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "16.04-LTS" version = "latest" } } variable "vms" { type = map(object( { vm_name = string location = string rg_name = string admin_username = string os_disk_name = string } )) } variable "admin_password" { type = string } variable "nic_ids" { type = list(string) } 

I am calling the modules in this file:

module "nic" { source = "app.terraform.io/******/nic/azurerm" version = "0.2.4" nics = var.nics } module "vm" { source = "app.terraform.io/******/vm/azurerm" version = "0.1.0" admin_password = var.admin_password vms = var.vms nic_ids = module.nic[each.value.id].nic_ids } 

I am defining variables like this:

nics = { nic1 = { nic_name = "test-nic-1" location = "eastUS2" rg_name = "test-rg" ipconfig-name = "nic-ipconfig-1" subnet-id = "/subscriptions/********/resourceGroups/test-rg/providers/Microsoft.Network/virtualNetworks/test-vnet/subnets/test-subnet" }, nic2 = { nic_name = "test-nic-2" location = "eastUS2" rg_name = "test-rg" ipconfig-name = "nic-ipconfig-2" subnet-id = "/subscriptions/**********/resourceGroups/test-rg/providers/Microsoft.Network/virtualNetworks/test-vnet/subnets/test-subnet" } } vms = { vm1 = { vm_name = "test-vm-1" location = "eastus2" rg_name = "test-rg" admin_username = "******" os_disk_name = "test-vm-1-osdisk" }, vm2 = { vm_name = "test-vm-2" location = "eastus2" rg_name = "test-rg" admin_username = "******" os_disk_name = "test-vm-2-osdisk" } } 

My aim is to pass NIC ids to the VM module and have 1:1 mapping between NIC id and VM (test-nic-1 should only be attached to vm-1, test-nic-2 to vm-2 etc.)

I am getting below error with this run:

Error: each.value cannot be used in this context on main.tf line 58, in module "vm": 58: nic_ids = module.nic[each.value.id].nic_ids A reference to "each.value" has been used in a context in which it unavailable, such as when the configuration no longer contains the value in its "for_each" expression. Remove this reference to each.value in your configuration to work around this error. 

Can anyone please suggest?

submitted by /u/Dry-Imagination1420
[link] [comments]

Read more here: https://www.reddit.com/r/Terraform/comments/kww71a/referencing_outputs_from_a_for_each_module/

Content Attribution

This content was originally published by /u/Dry-Imagination1420 at Terraform, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: