StorageAccountAlreadyTaken error at incremental de

2019-08-23 12:36发布

This template run on the same resource group produced the error StorageAccountAlreadyTaken but it is expected to do incremental deployment, ie. not to create any new resources. How to fix this?

{
  "type": "Microsoft.Storage/storageAccounts",
  "name": "[variables('prodSaName')]",
  "tags": { "displayName": "Storage account" },
  "apiVersion": "2016-01-01",
  "location": "[parameters('location')]",
  "sku": { "name": "Standard_LRS" },
  "kind": "Storage",
  "properties": {}
},
{
  "type": "Microsoft.Storage/storageAccounts",
  "name": "[ge.saName(parameters('brn'), parameters('environments')[copyIndex()])]",
  "tags": { "displayName": "Storage account" },
  "apiVersion": "2016-01-01",
  "location": "[parameters('location')]",
  "sku": { "name": "Standard_LRS" },
  "kind": "Storage",
  "copy": {
    "name": "EnvStorageAccounts",
    "count": "[length(parameters('environments'))]"
  },

Run New-AzureRmResourceGroupDeployment @args -debug with this template and it outputs:

New-AzureRmResourceGroupDeployment : 15:03:38 - Error: Code=StorageAccountAlreadyTaken; Message=The storage account named
UNIQUENAMEOFTHERESOURCE is already taken.
At C:\coding\gameo\gameoemergency\provision.run.ps1:101 char:9
+         New-AzureRmResourceGroupDeployment @args -debug
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-AzureRmResourceGroupDeployment], Exception
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdle
   t

PS. Somehow running from VSTS doesn't have this result. That run from local machine. Also there is no such error in Activity Log, strangely.

Update.

If I don't do these selects of a subscription as below but only for RM there are no errors.

    Select-AzureRmSubscription -SubscriptionID $Cfg.subscriptionId > $null

    # this seems to be the reason of the error, if removed - works:
    Select-AzureSubscription -SubscriptionId $Cfg.subscriptionId > $null

    # ... in order to run:
    exec { Start-AzureWebsiteJob @startArgs | out-null }

3条回答
欢心
2楼-- · 2019-08-23 12:53

Not sure if its a help but I had this issue with APIs prior to 2016-01-01 as there was a known bug. I updated to 2016-12-01 as this was the latest at the time and it worked for me.

Have you tried updating the api to the latest and trying again? The latest is 2018-02-01

查看更多
3楼-- · 2019-08-23 13:00

A good thing to do before performing a deployment is to validate the resources that if it can be created with the name provided. For EventHub and Storage Account there are built in cmdlets that allows you to check it.

For EventHub

Test-AzureRmEventHubName -Namespace $eventHubNamespaceName | Select-Object -ExpandProperty NameAvailable
        if ($availability -eq $true) {
            Write-Host -ForegroundColor Green `r`n"Entered Event Hub Namespace name is available"

For Storage Account

 while ($true) {
        Write-Host -ForegroundColor Yellow `r`n"Enter Storage account name (Press Enter for default) : " -NoNewline
        $storageAccountName = Read-Host
        if ([string]::IsNullOrEmpty($storageAccountName)) {
            $storageAccountName = $defaultstorageAccountName
        }
        Write-Host -ForegroundColor Yellow `r`n"Checking whether the entered name for Storage account is available"
        $availability = Get-AzureRmStorageAccountNameAvailability -Name $storageAccountName | Select-Object -ExpandProperty NameAvailable
        if ($availability -eq $true ) {
            Write-Host -ForegroundColor Green `r`n"Entered Storage account name is available"
            break
        }
        Write-Host `r`n"Enter valid Storage account name"
    }

This will prevent deployment errors by validating before deployment and asking the user to enter a valid name again if the entered one is not present.

查看更多
再贱就再见
4楼-- · 2019-08-23 13:06

This is the result of selecting a subscription twice for Resource Manager cmdlets and using Select-AzureSubscription as below:

Select-AzureRmSubscription -SubscriptionID $Cfg.subscriptionId > $null

# this seems to be the reason of the error, if removed - works:
# Select-AzureSubscription -SubscriptionId $Cfg.subscriptionId > $null

To avoid old cmdlets (before RM) we can use Invoke-AzureRmResourceAction. See example at https://stackoverflow.com/a/51712321/511144

查看更多
登录 后发表回答