How to reliably attach data disks in VMSS CustomScript extension


I'm trying to make a VMSS with persistent data disks and developed a PowerShell script to be executed in the CustomScript extension.

However, it occasionally fails to attach a disk to a VM instance when scaling up occurs with 4 or more instances at a time. I've investigated the problem and tried to mitigate the possible reasons but no luck so far.

Here's the explanation of the problem and its background project. It includes the ARM template with step by setup guide so that you can easily reproduce the problem.

Could someone please tell me any insights about it?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
6,846 questions
Azure Disk Storage
Azure Disk Storage
A high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution.
551 questions
Azure Virtual Machine Scale Sets
Azure Virtual Machine Scale Sets
Azure compute resources that are used to create and manage groups of heterogeneous load-balanced virtual machines.
332 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 12,961 Reputation points Microsoft Employee

    Hello @八重樫 剛史 Takeshi Yaegashi ,
    Thanks for the detailed description of the problem statement.
    Went through the setup.ps1 script, basically the problem is with $LUN variable which is passed as a parameter which will always have the value of 0. That variable value needs to be incremented by 1 every time when you try to attach a new data disk. Since it is trying to use the same number hence it is complaining about the conflict.

    Example Repro:

    When we run the command:
    Add-AzvmssVMDataDisk -VirtualMachineScaleSetVM $vm -ManagedDiskId $disk.Id -Caching "ReadOnly" -CreateOption Attach -Lun 0 | Update-AzvmssVM

    It will add and attach the data disk with LUN 0


    Now If i run the same command for the second disk

    Add-AzvmssVMDataDisk -VirtualMachineScaleSetVM $vm -ManagedDiskId $disk.Id -Caching "ReadOnly" -CreateOption Attach -Lun 0 | Update-AzvmssVM
    Update-AzvmssVM : A disk named 'testdisk1' already uses the same LUN: 0.
    ErrorCode: InvalidParameter
    ErrorMessage: A disk named 'testdisk1' already uses the same LUN: 0.
    ErrorTarget: dataDisk.lun
    StatusCode: 400
    ReasonPhrase: Bad Request
    OperationID : 831f1bcf-7fcb-4922-9d6f-00eef6d15fb5
    At line:1 char:130

    • ... -Caching "ReadOnly" -CreateOption Attach -Lun 0 | Update-AzvmssVM
    • ~~~~~~~~~~~~~~~
    • CategoryInfo : CloseError: (:) [Update-AzVmssVM], ComputeCloudException
    • FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.UpdateAzureRmVmssVM


    Now If i change the last command to use LUN 1

    Add-AzvmssVMDataDisk -VirtualMachineScaleSetVM $vm -ManagedDiskId $disk.Id -Caching "ReadOnly" -CreateOption Attach -Lun 1 | Update-AzvmssVM



    So the fix for your script should be to increment the value of LUN every time when a new data disk gets added before adding the next disk.

    All the very best !!

    If the above details help out in resolving your issue , please make sure to "Upvote & Accept the answer"