0

I am learning bicep and trying to get my head around loops. I have a basic integer based loop which will create x number of VMs based on the int value passed through as a parameter (aka if parameter value is 3, 3 VMs should be created). I am having trouble getting Bicep to allocate the VMs to the correct AZ. The code I have is below:

param location string
param vmname string
param tags object
param OSVersion string 
param compName string
param username string
@secure()
param password string
param vmCount int


resource virutalmachine 'Microsoft.Compute/virtualMachines@2022-03-01' = [for i in 
range(1, vmCount): {
name: '${vmname}${i}'
location: location
tags: tags

properties: {
  hardwareProfile: {
    vmSize: 'Standard_D2as_v4'
  }

  osProfile: {
    computerName: compName
    adminUsername: username
    adminPassword: password
  }

  storageProfile: {
    imageReference: {
      publisher: 'MicrosoftWindowsServer'
      offer: 'WindowsServer'
      sku: OSVersion
      version: 'latest'
    }

   osDisk: {
      createOption: 'FromImage'
      diskSizeGB: 128
      managedDisk: {
          storageAccountType: 'StandardSSD_LRS'
        }
      }
    }

  networkProfile: {
    networkInterfaces: [
        {
        id: nic[i].id
        }
      ]
    }
  }

zones: [
 '${i}'
 ]

}]

resource nic 'Microsoft.Network/networkInterfaces@2022-01-01' = 
[for i in range(0, vmCount): {
name: '${nicname}${i}'
location: location

properties: {
  ipConfigurations:[
    {
      name: 'ipconfig1'
      properties: {
        privateIPAllocationMethod: 'Dynamic'
        subnet: {
          id: subnetID
        }
  
      }
    }
  ]

  networkSecurityGroup: {
    id: nsgID
    }

  }
}]
Mr I
  • 23
  • 6
  • What is the error you are getting or what s not working ? – Thomas Aug 30 '22 at 19:26
  • also if you could share part of the template related to `nic` it will be helpful – Thomas Aug 31 '22 at 06:55
  • @Thomas When I run the above, it doesn't allocate the VMs to any AZs at all. I have also added the code for the nic (apologies for leaving this out) – Mr I Aug 31 '22 at 14:01
  • Does the region you're deploying to support AZs? – bmoore-msft Aug 31 '22 at 16:38
  • Yep it does support it. I can deploy fine via the Azure portal. This method I believe would never actually work as the first index value of "i" would be 0, if 0 was passed through to Azure as the target availability zone, then this would error. I think what I need to be to do is generate 1,2,3,1,2,3 each time the loop iterates. – Mr I Sep 01 '22 at 12:32

0 Answers0