-1

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'
    }
  }
]
3
  • you are already using a loop for disks. do you mean loop over the VM and the VM disks? Commented May 2 at 1:02
  • yes, this is only for 1 vm. I have 3 of them Commented May 2 at 1:30
  • 1
    Thomas, thanks. This worked perfectly. I'm just getting started with bicep, this helped tremenously. Commented May 4 at 15:27

1 Answer 1

0

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
    }
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.