title: Azure Terraform for private DNS and vnet links
body: <p>I am trying to create code to make creating azure private DNS zones and virtual network links. I am attempting to try to create a 2 variables, 1 with a list of private DNS zones and the other a list of our virtual network resource IDs. I can successfully run the code but my issue is that if I go to add any virtual networks, existing network peers want to be destroyed and recreated. while these will recreate fine, I am trying to avoid them being destroyed as it may cause issues with service. I am fairly new to terraform so any pointers would be helpful. I think my issue is within the count statement but unsure what I could do to fix it.</p>
  <p>main.tf</p>
  <pre><code>terraform {
    required_version = &quot;&gt;= 0.12&quot;
  }
  
  provider &quot;azurerm&quot; { 
    version   = &quot;&gt;= 2.0&quot;
  features {}
  }
  
  resource &quot;random_id&quot; &quot;random&quot; {
    count = length(var.private_zones) * length (var.linked_virtual_networks)
    byte_length = 2
  
  }
  
  resource &quot;azurerm_resource_group&quot; &quot;privatezones&quot; {
    name     = &quot;it-eastus-prod-privatedns&quot;
    location = &quot;eastus&quot;
    tags     = var.tags
  }
  
  
  resource azurerm_private_dns_zone private_dns {
    count                = length(var.private_zones)
    name                 = var.private_zones[count.index]
    resource_group_name  = azurerm_resource_group.privatezones.name
    tags                 = var.tags
  }
  
  resource &quot;azurerm_private_dns_zone_virtual_network_link&quot; &quot;example&quot; {
    count                 = length(var.private_zones) * length (var.linked_virtual_networks)
    name                  = &quot;vnet-${random_id.random.*.dec[count.index]}&quot;
    resource_group_name   = azurerm_resource_group.privatezones.name
    private_dns_zone_name = azurerm_private_dns_zone.private_dns[floor(count.index/length (var.linked_virtual_networks))].name
    virtual_network_id    = var.linked_virtual_networks[count.index % length (var.linked_virtual_networks)]
    tags                  = var.tags
  }
  
  </code></pre>
  <p>variables.tf</p>
  <pre><code>variable private_zones {
      type        = list(string)
      description = &quot;List of private dns zones to be created&quot;
      default = [
              &quot;privatelink.database.windows.net&quot;,
              &quot;privatelink.vaultcore.azure.net&quot;,
              #&quot;privatelink.eastus.azmk8s.io&quot;,
              #  &quot;privatelink.westus.azmk8s.io&quot;,
              #  &quot;privatelink.blob.core.windows.net&quot;,
              #  &quot;privatelink.table.core.windows.net&quot;,
              #  &quot;privatelink.queue.core.windows.net&quot;,
              #  &quot;privatelink.file.core.windows.net&quot;,
              #  &quot;privatelink.web.core.windows.net&quot;
              ]
   }
  
   variable linked_virtual_networks {
      type        = list(string)
      description = &quot;List of linked virtual networks&quot;
      default = [
                 &quot;/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworks/xxx&quot;,
                  &quot;/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworks/xxx&quot;,
              ]
   }
  
  
   variable &quot;tags&quot; {
    description = &quot;tags&quot;
    default     = {
             &quot;applicationname&quot; = &quot;DNS&quot;
             &quot;creationdate&quot;    = &quot;7/27/2020&quot;
             &quot;creator&quot;         = &quot;name&quot;
             &quot;environment&quot;     = &quot;Production&quot;
             &quot;owner&quot;           = &quot;Net&quot;
     }
   }
  </code></pre>