Coder Perfect

How can I create a kid resource in Azure Bicep for each of my current parent resources?


My actual use case is as follows: I’m building a Bicep file that adds a subnet to an existing VNET and then adds equivalent VNET rules to a list of Azure SQL Servers. A configuration parameter should be used to specify the SQL Servers.

I tried to use a loop for those rules:

param azureSqlVnetRules array = [
    azureSqlServerName: 'Azure SQL server accessed by Aks'
    // ... other properties here

resource azureSqlServer 'Microsoft.Sql/servers@2021-05-01-preview' existing = [for rule in azureSqlVnetRules: {
  name: rule.azureSqlServerName

  resource vnetRule 'virtualNetworkRules' = {
    // VNET rule properties go here

Because of BCP160, a nested resource cannot be contained within a resource with a for-expression. “Can’t loop a resource with nested child resources,” says the documentation.

“Change the kid resources to top-level resources,” the documents advise. However, how do I go about doing this in my situation? I don’t want to add many kid resources to one parent resource, as the example suggests, but rather one child resource to many parent resources.

Asked by Fabian Schmied

Solution #1

I already found a solution while writing this question:—one-child-resource-per-parent The concept is to loop twice, synchronizing the results of the first loop with the results of the second loop using indexes:

resource azureSqlServers 'Microsoft.Sql/servers@2021-05-01-preview' existing = [for i in range(0,  length(azureSqlVnetRules)): {
  name: azureSqlVnetRules[i].azureSqlServerName

resource vnetRules 'Microsoft.Sql/servers/virtualNetworkRules@2021-05-01-preview' = [for i in range(0,  length(azureSqlVnetRules)): {
  parent: azureSqlServers[i]
  // VNET rule properties go here

Answered by Fabian Schmied

Post is based on