Reputation: 664
I have been working on this terraform azure vm template and the goal is use for_each to make the module more dynamic in nature but i'm not able to figure out how to reference one resource_id in another resource block.
If you see in first resource block i'm creating NICs using "for_each" and i want to reference the "network_interface_id" in second resource which is associating Network Interface to outbound load balancer. Not sure how to do that but started to construct the string using variables. can anyone help me on how to reference the "network_interface_id" or any other resource id if required, any help is highly appreciated.
###################
# Network Interface
###################
resource "azurerm_network_interface" "this" {
for_each = var.vm_details
name = format(
"%s-${var.location}-%s-%s-nic-%s",
var.app_acronym,
var.env,
var.app_purpose,
each.value.vm_identifier
)
location = var.location
resource_group_name = var.resource_group_name
tags = var.tags
ip_configuration {
name = format(
"%s-${var.location}-%s-%s-ip-%s",
var.app_acronym,
var.env,
var.app_purpose,
each.value.vm_identifier
)
subnet_id = var.subnet_id
private_ip_address_allocation = var.private_ip_address_allocation != "" ? var.private_ip_address_allocation : "Dynamic"
}
enable_accelerated_networking = each.value.enable_accelerated_networking
}
###########################################################
# Asssociating Network Interface to outbound load balancer
###########################################################
resource "azurerm_network_interface_backend_address_pool_association" "this" {
for_each = var.olb_association
network_interface_id = "${var.rsrc_id_str_1}${var.subscription_id}${var.rsrc_id_str_2}${var.resource_group_name}${var.rsrc_id_str_nic_3}${var.app_acronym}${var.hifen}${var.location}${var.hifen}${var.env}${var.hifen}${var.app_purpose}${var.nic_abbrv}${each.value.vm_identifier}"
ip_configuration_name = format(
"%s-${var.location}-%s-%s-ip-%s",
var.app_acronym,
var.env,
var.app_purpose,
each.value.vm_identifier
)
backend_address_pool_id = each.value.backend_address_pool_id
depends_on = [azurerm_network_interface.this]
}
Upvotes: 0
Views: 415
Reputation: 10087
for_each
creates a data structure that is referenced like the map you feed it. So if the name for an entry is "puppy", you would reference it as azurerm_network_interface.this["puppy"]
Upvotes: 2