Home > OS >  Referencing map objects in TF
Referencing map objects in TF

Time:12-28

I need feedback. I have the below partial tf code. I am getting "Error: Incorrect attribute value type... Inappropriate value for attribute "security_rule": set of object required.". Looks like the reference to the map value is not set correctly but can't seem to figure it out. What am I missing? Thanks

SAMPLE CODE

input.tfvars
-----------------

test_nsg = {
  "testnsg_1" = {
    location = "West US"
    rules = {
      "AllOutbound" = {
        priority                   = 300
        direction                  = "Outbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "*"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
      },
      "AllowSSH" = {
        priority                   = 400
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
      },
    }
  },
  "testnsg_2" = {
    rules = {}
  },
  "testnsg_3" = {
    rules = {
      "AllOutbound" = {
        priority                   = 500
        direction                  = "Outbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "*"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
      },
    }
  },
}


nsg.tf
-------

resource "azurerm_network_security_group" "nsg" {
  for_each = var.test_nsg

  name                = each.key
  location            = var.location
  resource_group_name = var.rg_name
  tags                = var.tags
  security_rule       = each.value.rules
}

CodePudding user response:

It should be List of objects, and in your case it is a map of objects. I think the following should work:

resource "azurerm_network_security_group" "nsg" {
  for_each = var.test_nsg

  name                = each.key
  location            = var.location
  resource_group_name = var.rg_name
  tags                = var.tags
  security_rule       = values(each.value.rules)
}
  • Related