Hi Folks,
This is a strinctly non-functional issue, only a cosmetic one.
Time to time I need to create maps, where the unique key contains the ID of resource. This makes the keys very difficult to read in logs and console.
For example:
nic_asg_association = {
+ "NIC_NAME-/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Network/applicationSecurityGroups/APPSEC_GROUP_NAME" = {
+ application_security_group_id = "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Network/applicationSecurityGroups/APPSEC_GROUP_NAME"
+ name = "NIC_NAME"
}
+ "NIC_NAME-/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Network/applicationSecurityGroups/APPSEC_GROUP_NAME" = {
+ application_security_group_id = "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Network/applicationSecurityGroups/APPSEC_GROUP_NAME"
+ name = "NIC_NAME"
}
}
This particular output is generated with:
locals {
nic_default_asg_association = {
for asg in var.nic_default_interface.application_security_group_ids :
"${var.nic_default_interface.name}-${asg}" => {
name = var.nic_default_interface.name
application_security_group_id = asg
}
}
nic_additional_asg_association = merge([
for iface in var.nic_additional_interface :
iface.application_security_group_ids == null ? {} : {
for asg in iface.application_security_group_ids :
"${iface.name}-${asg}" => {
name = iface.name
application_security_group_id = asg
}
}
]...)
nic_asg_association = merge(local.nic_default_asg_association, local.nic_additional_asg_association)
}
I have tried to simplify the key for better visuals by using string functions during computation of the map. The code runs without error, but nothing happens.
with replace
"${replace(var.nic_default_interface.name, "-.*$", "")}-${asg}"
"${replace(iface.name, "-.*$", "")}-${asg}"
"${replace("${var.nic_default_interface.name}-${asg}", "/.*asgr-", "asgr-")}"
"${replace("${iface.name}-${asg}", "/.*asgr-", "asgr-")}"
Tried also on just a part of the key
"${var.nic_default_interface.name}-${replace(asg, "/[^/]*$", "")}"
"${iface.name}-${replace(asg, "/[^/]*$", "")}"
with element
element(split("/asgr-", "${var.nic_default_interface.name}-${asg}"), 1)
Question:
Am I doing something wrong, or terraform just cant handle to call functions in this situation.
Thanks,
Leder