How to pass duplicate object keys into NSG rules deploy?

Hello forum,

I am using Terraform version 0.0.142 and azurerm to try and deploy some network security group (nsg) rules for my pipeline but I’m having some trouble with duplicate keys in my nsg rules.
Here’s my sample main.tf:

resource “azurerm_network_security_rule” “nsg_rule1” {
for_each = {for nsg_rule1 in var.nsg_values:nsg_rule1.name => nsg_rule1}
name = each.value.name
priority = each.value.priority
protocol = “*”
source_address_prefix = each.value.source_address_prefix

variable.tf sample file:
variable "nsg_values: {
type = list(object({
name = string
priority = string
source_address_prefix = string
}))
}

tfvars array of values:
nsg_values = [
{
name = “RDP”
priority = “2500”
source_address_prefix = “"
},
{
name = “RDP1”
priority = “2600”
source_address_prefix = "

}
]

Understandably, my list of object only wants me to pass unique values but I have some nsg rules which allows or denies traffic from all or any address spaces. Is there a way for me to have my config file deal with duplicate keys?
Thanks.

Would it be an option to use the unique priority instead of the name? AFAIK the priority is unique within Azure NSG.

for_each = {for nsg_rule1 in var.nsg_values:nsg_rule1.priority => nsg_rule1}
1 Like

Thanks very much @tbugfinder. Substituting ‘name’ with ‘priority’ in my for_each with for statement did the trick. Since I had one case of duplicate priority among my nsg rules, I just went in and gave it a different priority # and my pipeline ran without issues.
Very helpful tip. :slight_smile: