Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.23k stars 748 forks source link

How to add dependency on self while in loop #4494

Closed jobinjosem closed 3 years ago

jobinjosem commented 3 years ago

Bicep version Bicep CLI version 0.4.613

Describe the bug I am trying to create an automation account and import a few modules. The automation account is created successfully but some modules has dependency on other module How to add a dependency on those modules. I know we can do it while we add each module as separate modules but I am trying to import all modules in a single module using for loop.

To Reproduce

var modulestoimport = [
  {
    name: 'AZ.Accounts'
    uri: 'https://www.powershellgallery.com/api/v2/package/Az.Accounts/2.5.3'
    version: '2.5.3'
  }
  {
    name: 'AZ.Network'
    uri: 'https://www.powershellgallery.com/api/v2/package/Az.Network/4.11.0'
    version: '4.11.0'
  }
  {
    name: 'AZ.Storage'
    uri: 'https://www.powershellgallery.com/api/v2/package/Az.Storage/3.5.0'
    version: '3.5.0'
  }
]

resource acmeautomation 'Microsoft.Automation/automationAccounts@2020-01-13-preview' = {
  name: 'acme-test-aua'
  location: resourceGroup().location
  properties: {
    publicNetworkAccess: true
    sku: {
      name: 'Free'
    }
  }
}

resource auamodules 'Microsoft.Automation/automationAccounts/modules@2020-01-13-preview' = [for moduletoimport in modulestoimport: {
  name: '${acmeautomation.name}/${moduletoimport.name}'
  dependsOn: [
    acmeautomation
    (moduletoimport.name == 'AZ.Accounts') ? json('null') : moduletoimport[0].name
  ]
  properties: {
    contentLink: {
      uri: moduletoimport.uri
      version: moduletoimport.version
    }
  }
}]

Here I am trying to import the modules but the Az.Network and AZ.Storage has a dependency on AZ.Accounts. So I have to add that dependency only while network and storage modules are getting imported.

alex-frankel commented 3 years ago

Can you try adding @batchSize(1) about the auamodules resource declaration? This will ensure the modules deploy serially instead of in parallel.

@batchSize(1)
resource auamodules '...' = ...
jobinjosem commented 3 years ago

This seems to be working @alex-frankel Thank you.