Combine information from two resources

I need to pass information from my web module to my app_gateway module to associate each NIC to a backend pool, and to use the VM names for overriding host names.

I have been able to do the latter by just doing the following:

output "vm_list" {
	value = azurerm_virtual_machine.main

This outputs the list of 3 VMs that I can then pass as a variable into the other module. Is there a way that I can combine the NIC information with the VM information to create only one mapped output?

resource "azurerm_network_interface" "main" {
  count               = var.vm_count
  name                = "${format("${local.resource_prefix}%03d", count.index)}-nic"
  location            = azurerm_resource_group.main.location
  resource_group_name =

  ip_configuration {
    name                          = "ipconfig"
    subnet_id                     = var.azurerm_subnet_id
    private_ip_address_allocation = "Dynamic"

  tags = local.common_tags

resource "azurerm_virtual_machine" "main" {
  count                 = var.vm_count
  name                  = format("${local.resource_prefix}%03s", count.index)
  location              = azurerm_resource_group.main.location
  resource_group_name   =
  network_interface_ids = [element(azurerm_network_interface.main.*.id, count.index)]
  vm_size               = var.azurerm_vm_size
  availability_set_id   =

I want to group VM name, VM id, NIC id, and ipaddress together as an output. Is this possible?

Hi @rstra!

What you are describing should definitely be possible, but I’m afraid I’m not familiar enough with these two resource types in particular to be able to sketch an example here. Would you mind showing an example of what you’d expect the resulting data structure to look like, and specifically which of the attributes of azurerm_network_interface and azurerm_virtual_machine produce the values you want to include?

By looking at the two resources I posted earlier, I am hoping to somehow merge the following:


I was hoping to be able to access the data in my other module by using var.vm_list.nic_id or something else similar to that.

Is there a way I can group that inside one single output?

Thank you.

Hi again @rstra! Thanks for the extra information.

I think the following should do what you want here:

output "virtual_machines" {
  value = [
    for i, vm in azurerm_virtual_machine.main : {
      name =
      id   =

      ip_address           = azurerm_network_interface.main[i].private_ip_address
      network_interface_id = azurerm_network_interface.main[i].id

The above is using a for expression, and relying on the fact that both of the resources have the same count expression and are correlated by their count.index values.

A resource with count set appears in expressions as a list of objects, so i in the above expression is the index into the list – the same as count.index in the resource blocks themselves – and vm is the instance of azurerm_virtual_machine.main that has index i, so we can use to access the name attribute of each object.

The result is a list of objects, again with the same indices as the original resources, where each one has four attributes capturing the values you asked about. If this module were being called using a module "example" block in a parent module, you could then access lists of individual attributes from module.example using Splat Expressions. For example, to derive just a list of all of the IP addresses, ignoring the other attributes: