Create Multiple NSG rules using for_each block

Hi All,

I am stuck with an logic for creating multiple NSG rules. I have close to 75 ports which has to be either allowed or denied in my project. I have created the following code to use the for_each block and refer the arguments required for creating the NSG rule, but it needs all the data updated in variable file. Its quite messy and difficult to manage. Is there a way to provide the ports in a list and parse it while creating them instead? I am sure this must have been addressed earlier, but couldn't find anything matching my issue.

resource "azurerm_network_security_rule" "testrules" {

for_each = local.nsgrules

name = each.key

direction = each.value.direction

access = each.value.access

priority = each.value.priority

protocol = each.value.protocol

source_port_range = each.value.source_port_range

destination_port_range = each.value.destination_port_range

source_address_prefix = each.value.source_address_prefix

destination_address_prefix = each.value.destination_address_prefix

resource_group_name = "AUTOMATION_TESTRG"

network_security_group_name =




submitted by /u/janapb_devops
[link] [comments]

Read more here:

Content Attribution

This content was originally published by /u/janapb_devops at Terraform, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: