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?
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?
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 = vm.name
id = vm.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 vm.name 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: