How to check if resource already exists in the ARM template

Eashendra Singh 5 Reputation points
2023-05-20T07:02:17.4433333+00:00

I would like to automate the process of ARM deployment in which resource should not get deployed if it already exists.

Trying to implement the below code but getting an error

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "newOrExisting": {
      "type": "string",
      "defaultValue": "new",
      "allowedValues": [
        "new",
        "existing"
      ]
    }
  },
  "resources": {
    "saNew": {
      "condition": "[equals(parameters('newOrExisting'), 'new')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2"
    },
    "saExisting": {
      "condition": "[equals(parameters('newOrExisting'), 'existing')]",
      "existing": true,
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[parameters('storageAccountName')]"
    }
  },
  "outputs": {
    "storageAccountId": {
      "type": "string",
      "value": "[if(equals(parameters('newOrExisting'), 'new'), resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')))]"
    }
  }
}

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/conditional-resource-deployment

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
911 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Konstantinos Passadis 19,171 Reputation points MVP
    2023-05-20T09:19:13.5633333+00:00

    Hello

    I understand that you want to validate whether a resource exists already before deploying an ARM template within the JSON.

    ARM Templates cannot do that. You have to find a new method

    For the provide code :

    In the following ARM template, if the 'newOrExisting' parameter is set to 'new', then the storage account will be created. If it is set to 'existing', the storage account will not be created.

    json
    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "storageAccountName": {
          "type": "string"
        },
        "location": {
          "type": "string",
          "defaultValue": "[resourceGroup().location]"
        },
        "newOrExisting": {
          "type": "string",
          "defaultValue": "new",
          "allowedValues": [
            "new",
            "existing"
          ]
        }
      },
      "resources": [
        {
          "condition": "[equals(parameters('newOrExisting'), 'new')]",
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2022-09-01",
          "name": "[parameters('storageAccountName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "StorageV2"
        }
      ],
      "outputs": {
        "storageAccountId": {
          "type": "string",
          "value": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
        }
      }
    }
    
    
    
    

    User's image

    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.