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
}
}
}]
Where an indeterminate number of VMs should be deployed in to alternate AZs, where there are 3 AZs, based on the iteration of a loop, the only modification required to the above code is:
Assuming you're not trying to deploy thousands of VMs, or some other weird edge case, this will cycle through the AZs 1, 2, 3, 1, 2, 3, 1... etc. as your loop iterates.
You can see this with the following bicep:
This will output the following:
HTH someone.