title: Terraform: Specify network security group rules when creating an AKS cluster using azurerm_kubernetes_cluster
body: <p>I am using the Terraform azurerm provider version 1.19 to create an AKS cluster. I'd like to specify network security group rules when creating the cluster but I can't figure out how to reference the security group that is created since the generated security group is given a name with random numbers.</p>
  
  <p>Something like:</p>
  
  <blockquote>
    <p>aks-agentpool-33577837-nsg</p>
  </blockquote>
  
  <p>Is there a way to reference the created nsg or atleast output the random number used in the name?</p>
  
  <p>Configuration to create the cluster:</p>
  
  <pre><code>resource "azurerm_resource_group" "k8s" {
    name     = "${var.resource_group_name}"
    location = "${var.location}"
  }
  
  resource "azurerm_kubernetes_cluster" "k8s" {
    name                = "${var.cluster_name}"
    location            = "${azurerm_resource_group.k8s.location}"
    resource_group_name = "${azurerm_resource_group.k8s.name}"
    dns_prefix          = "${var.dns_prefix}"
    kubernetes_version  = "${var.kubernetes_version}"
  
    linux_profile {
      admin_username = "azureuser"
  
      ssh_key {
        key_data = "${file("${var.ssh_public_key}")}"
      }
    }
  
    agent_pool_profile {
      name    = "default"
      count   = "${var.agent_count}"
      vm_size = "${var.vm_size}"
      os_type = "Linux"
    }
  
    service_principal {
      client_id     = "${var.client_id}"
      client_secret = "${var.client_secret}"
    }
  
    tags {
      source      = "terraform"
      environment = "${var.environment}" 
    }
  }
  </code></pre>
  
  <p>This generates a security group which I'd like to add additional rules to. Here's a rule I'd like to add so the nginx-controller's liveness probe can be checked.</p>
  
  <pre><code>resource "azurerm_network_security_rule" "nginx_liveness_probe" {
    name                        = "nginx_liveness"
    priority                    = 100 
    direction                   = "Inbound"
    access                      = "Allow"
    protocol                    = "Tcp"
    source_port_range           = "*"
    destination_port_range      = "${var.nginx_liveness_probe_port}"
    source_address_prefix       = "*"
    destination_address_prefix  = "*"
    resource_group_name         = "${azurerm_kubernetes_cluster.k8s.node_resource_group}"
    network_security_group_name = How do I reference the auto-generated nsg ?
    description = "Allow access to nginx liveness probe"
  }
  </code></pre>