Search code examples
azure-devopsazure-storageazure-storage-queues

Create Incremental Azure Storage Accounts on every run


Looking for a script to autogenerate a new storage account with a prefix dev something like dev01.... and when i rerun the template should increment as dev02.. on second run and so on. I tried giving the parameters / using default templates in github. Issue is if i pass on a value under the value the system deploys it fine ,if i give the same name it would rerun and update the existing storage. Instead i would like it to check if storage account exists and if not create a new account , please advise any pointers if any

Sample Parameters.Json file im using :

"parameters": {

    "storageAccountName": {
        "value": "dev01"
    },

Solution

  • I recommand that you could use the Azure powershell script to customize your logic to do that. The following is the demo code:

    $resourceGroup = "rgName"
    $storageAccount = "accountName"
    $location = "Central US"
    $SkuName = "Standard_LRS"
    $kind = "StorageV2"
    $i = 0;   
    while(1)
    {
       $storage = Get-AzureRmStorageAccount  -ResourceGroupName $resourceGroup -Name $storageAccount 
       if($storage -ne $null)
        { 
          $i++
          $storageAccount = $storage.StorageAccountName + $i
          $storage = New-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount -SkuName $SkuName  -Location $location -Kind $kind
        }
        else
        {
          $storage = New-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount -SkuName $SkuName  -Location $location -Kind $kind 
          $storageAccount = $storageAccount +$i;
    
        }
    
       if ($storage -ne $null)
       {
         break;
       }
    
    }
    

    Task:

    enter image description here