I have 3 VMs - eusvmdev01-3
Each VM has 6 attached data disks
The code below creates 6 disks for the first VM.
Is there an easier way to create the rest with a loop?
param location string
resource sharedDisk 'Microsoft.Compute/disks@2024-03-02' = [
for i in range(1, 6): {
location: location
name: 'eusvmdev01-data${i}'
properties: {
creationData: {
createOption: 'Empty'
}
diskSizeGB: 127
osType: 'Linux'
}
sku: {
name: 'Standard_LRS'
}
}
]
You can create a module to create the disk and attach the disks to the vm.
// Module vm.bicep
param location string
param vmPrefix string
param vmIdex int
// Create disks
resource disks 'Microsoft.Compute/disks@2024-03-02' = [
for i in range(1, 6): {
location: location
name: '${vmPrefix}${vmIdex}-data${i+1}'
properties: {
creationData: {
createOption: 'Empty'
}
diskSizeGB: 127
osType: 'Linux'
}
sku: {
name: 'Standard_LRS'
}
}
]
// Create vm and attach disks
resource vm 'Microsoft.Compute/virtualMachines@2024-11-01' = {
name: '${vmPrefix}${vmIdex}'
location: location
properties: {
storageProfile: {
dataDisks: [
for i in range(0, 5): {
lun: i
createOption: 'Attach'
managedDisk: {
id: disks[i].id
}
}
]
}
...
}
}
Then you can invoke it multiple times from your main bicep:
// main.bicep
param location string = resourceGroup().location
param vmPrefix string = 'eusvmdev0'
// Create 3 VMs
module vms 'vm.bicep' = [
for i in range(1, 3): {
params: {
location: location
vmPrefix: vmPrefix
vmIdex: i
}
}
]