Home > Software engineering >  Azure Bicep - Why does the module dependency not work?
Azure Bicep - Why does the module dependency not work?

Time:06-16

I am new to bicep and trying to use modules to create a VNet and a subnet. I have the following two modules

vnet.bicep

param name string
param location string
param addressPrefixes array
param tags object

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
  name: name
  location: location
  properties: {
    addressSpace: {
      addressPrefixes: addressPrefixes
    }
  }

  tags: tags
}

subnet.bicep

param parentVnetName string
param name string
param addressPrefix string

resource parentVnet 'Microsoft.Network/virtualNetworks@2020-11-01' existing = {
  name: parentVnetName
}

resource subnet 'Microsoft.Network/virtualNetworks/subnets@2021-08-01' = {
  name: name
  parent: parentVnet
  properties: {
    addressPrefix: addressPrefix
  }
}

I am trying the use the below config to create a vnet and multiple subnets.

param location string = 'uksouth'

param vnet object = {
  name: 'vnet'
  addressPrefixes: [
    '10.0.0.0/16'
  ]
  subnets: [
    {
      name: 'subnet1'
      addressPrefix: '10.0.1.0/24'
    }
    {
      name: 'subnet2'
      addressPrefix: '10.0.2.0/24'
    }
  ]
}

targetScope = 'subscription'

resource rg 'Microsoft.Resources/resourceGroups@2021-01-01' = {
  name: 'rg'
  location: location
  tags: {}
}

module vnetsym 'modules/vnet.bicep' = {
  scope: rg
  name: vnet.name
  params: {
    name: vnet.name
    location: location
    addressPrefixes: vnet.addressPrefixes
    tags: {}
  }
}

module subnetsym 'modules/subnet.bicep' = [for subnet in vnet.subnets: {
  scope: rg
  name: subnet.name
  params: {
    name: subnet.name
    addressPrefix: subnet.addressPrefix
    parentVnetName: vnet.name
  }
  dependsOn: [
    vnetsym
  ]
}]

However, bicep seems to fail to work out the dependencies and throws the below error. There are no other operations in progress but this deployment. Why does the module dependency not work in this case and how do I fix it?

{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"Conflict","message":"{\r\n  \"status\": \"Failed\",\r\n  \"error\": {\r\n    \"code\": \"ResourceDeploymentFailure\",\r\n    \"message\": \"The resource operation completed with terminal provisioning state 'Failed'.\",\r\n    \"details\": [\r\n      {\r\n        \"code\": \"DeploymentFailed\",\r\n        \"message\": \"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.\",\r\n        \"details\": [\r\n          {\r\n            \"code\": \"Conflict\",\r\n            \"message\": \"{\\r\\n  \\\"error\\\": {\\r\\n 
\\\"code\\\": \\\"AnotherOperationInProgress\\\",\\r\\n    \\\"message\\\": \\\"Another operation on this or dependent resource is in progress. To retrieve status of the operation use uri: https://management.azure.com/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Network/locations/uksouth/operations/ef9f087b-0448-44a2-ab05-9579837bf9de?api-version=2021-08-01.\\\",\\r\\n    \\\"details\\\": []\\r\\n  }\\r\\n}\"\r\n          }\r\n        ]\r\n      }\r\n    ]\r\n  }\r\n}"}]}}

I've also noticed that the behaviour is inconsistent. Sometimes no subnets are created, sometimes just one subnet is created. I have also been able to complete the deployment successfully on occasion by deploying multiple times!

CodePudding user response:

The issue here is that the subnet modules are deployed in parallel. They both try to update the vnet at the same resulting in the conflict error you're seeing.

In you case you can use a module decorator:

By default, resources are deployed in parallel. When you add the batchSize decorator, you deploy instances serially.

So this should work:

@batchSize(1)
module subnetsym 'modules/subnet.bicep' = [for subnet in vnet.subnets: {
...

Also you should be deploying the subnets inside the vnet resource. If you re-run your template, it will delete the subnets before recreating them. There are few issues related:

  • Related