I am trying to add a variable called 'DeployNSG' as a true/false Boolean. When I reference the variable in the resource creation for the NSG using 'Count', I am then trying to associate the NSG with Azurerm_Network_security_group_association with a subnet and it's saying that I need to use count index in the association.. however If I then try and use element to reference an item, it says you can't use element if count isn't used within the subnet association.
resource "azurerm_network_security_group" "ProdNSG" {
count = "${var.DeployNSG ? 1 : 0}"
name = "${var.ProdNSG}"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.ProdNetworkRG.name}"
security_rule {
name = "AllowRDP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_virtual_network" "ProdVNet" {
name = "${var.ProdVNet}"
resource_group_name = "${azurerm_resource_group.ProdNetworkRG.name}"
address_space = "${var.ProdVNetAddressSpace}"
location = "${var.location}"
}
resource "azurerm_subnet" "ServersSubnet" {
resource_group_name = "${azurerm_resource_group.ProdNetworkRG.name}"
name = "${var.ServersSubnet}"
address_prefix = "${var.ServersSubnetAddressPrefix}"
virtual_network_name = "${azurerm_virtual_network.ProdVNet.name}"
}
resource "azurerm_subnet_network_security_group_association" "ServersNSGAssociation" {
subnet_id = "${azurerm_subnet.ServersSubnet.id}"
network_security_group_id = "${azurerm_network_security_group.ProdNSG.id}"
}
True/False condition works if I comment out the association, therefore I believe this is where it is stuck.