1
votes

I am new to ARM. I have created an ARM Template for deploying storage accounts and a data factory in a resource group.

If I want to use the same ARM Template to add another resource into the same resource group, can I not simply add that resource in the ARM Template below the storage accounts and data factory deployment code and run the ARM template?
Because currently, when I run the same template it is giving me an error saying:

storage accounts already exists.

Seems like it is trying to redeploy the storage accounts, which I don't want. How can I use the same ARM Template every time I have to deploy some new resource, by avoiding redeployment of already deployed resources?
Is there anything I can add in the ARM Template ?

Note: I don't wish to use PowerShell or Azure CLI here. I am deploying the resources using a pipeline where I have created YAML tasks.
PFB my sample template :

{
   "$schema": "http://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {
     "location": {
       "type": "string",
       "defaultValue": "West Europe"
     },
     "storageAccountName": {
        "type": "string",
        "defaultValue": "storageabc"
      }
     "accountType": {
       "type": "string"
     },
     "kind": {
       "type": "string"
     }
     ...
   },
   "resources": [
     {
       "name": "[parameters('storageAccountName')]",
       "type": "Microsoft.Storage/storageAccounts",
       "apiVersion": "2019-06-01",
       "location": "[parameters('location')]",
       "properties": {},
       "dependsOn": [],
       "sku": {
         "name": "[parameters('accountType')]"
       },
       "kind": "[parameters('kind')]"
     },
     {
        "type": "Microsoft.DataFactory/factories,
        "apiVersion": "2018-06-01"
        -
        -
        -
        ..
        
     }
   ],
   "outputs": {}
}
1
The same template should work? We deploy ARM templates containing e.g. Storage accounts daily and don't get this error. Can you give a sample of your template and how you are deploying it?juunas
@juunas thanks for responding. I have added a sample of my template. So if I have to add a new resource in this same template below the datafactory resource, and run this template, it tries to redeploy the storage accounts.Ganesh
ARM templates are idempotent, so you can run them as many times as you want they should give the same results, if you can provide the full template you are trying to deploy that would be much easier to judge your questionBruno
@Ganesh are you trying to redeploy in a different resource group ?Thomas
@Thomas, no I was trying to deploy in the same resource group. The answer provided by Miq below helped to resolve this issue. Any other suggestions will be appreciated :)Ganesh

1 Answers

1
votes

Storage account names have to be globally unique. Perhaps someone had already use that name, hence the message.

Common pattern is to use uniqueString(resourceGroup().id) function to reduce the probability of a clash.

If you are sure this name is unique, make sure you redeploy to this same resource Group and subscription. If you try to do a redeploy to a different resource group or subscription, Azure will try to create new resources and will throw you an error like you receive.

Template deployments are scoped, so if you change the scope (in this case resource group), you will get a new set of resources based on this template. For resources which names must be unique among entire Azure (i.e. storage account or container registry, web app, function app, etc.) best practice is to use hash based on the resource group id. The I’d contains guid of the subscription, which Azure makes sure is unique, so the probability of a clash is very low.