Search code examples
jsonazureazure-resource-managerazure-rm-template

I am trying to deploy an Azure SQL VM with ARM template. I am getting error as The template reference 'ion5eddb999' is ambiguous


The full error is below.

New-AzResourceGroupDeployment : 23:35:36 - Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template reference 'ion5eddb999' is ambiguous: there are multiple template 
resources '/subscriptions/d2143d4c-a258-4a38-8fbf-de0e42756e22/resourceGroups/sumantest/providers/Microsoft.Compute/virtualMachines/ion5eddb999,/subscriptions/d2143d4c-a258-4a38-8fbf-de0e42756e22/resou 
rceGroups/sumantest/providers/Microsoft.SqlVirtualMachine/SqlVirtualMachines/ion5eddb999' defined with this name. Please use fully qualified resource identity instead. Please see 
https://aka.ms/arm-template-expressions/#reference for usage details.'.

Below is the ARM template. I have tried using hard coded sqlserver name as well, but same error. Please let me know what is wrong I am doing here. Any help is appreciated.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "virtualMachineName": {
      "type": "String",
      "defaultValue": "ion5eddb999",
      "metadata": {
        "description": "The name of the VM"
      }
    },
    "virtualMachineSize": {
      "type": "String",
      "defaultValue": "Standard_B4ms",
      "metadata": {
        "description": "The virtual machine size."
      }
    },
    "ipAddress":{
        "type":"string",
        "defaultValue": "172.31.172.99",
        "metadata": {
            "description": "The virtual machine ip address"
        }
    },
    "existingVirtualNetworkName": {
      "type": "String",
      "defaultValue": "ion5ed-vnet",
      "metadata": {
        "description": "Specify the name of an existing VNet in the same resource group"
      }
    },
    "existingVnetResourceGroup": {
      "type": "String",
      "defaultValue": "ion5ed-gateway",
      "metadata": {
        "description": "Specify the resrouce group of the existing VNet"
      }
    },
    "existingSubnetName": {
      "type": "String",
      "defaultValue": "ion5ed-sub-devtest",
      "metadata": {
        "description": "Specify the name of the Subnet Name"
      }
    },
    "imageOffer": {
      "type": "String",
      "defaultValue": "sql2019-ws2019",
      "allowedValues": [
        "sql2019-ws2019",
        "sql2017-ws2019",
        "SQL2017-WS2016",
        "SQL2016SP1-WS2016",
        "SQL2016SP2-WS2016",
        "SQL2014SP3-WS2012R2",
        "SQL2014SP2-WS2012R2"
      ],
      "metadata": {
        "description": "Windows Server and SQL Offer"
      }
    },
    "sqlSku": {
      "type": "String",
      "defaultValue": "Standard",
      "allowedValues": [
        "Standard",
        "Enterprise",
        "SQLDEV",
        "Web",
        "Express"
      ],
      "metadata": {
        "description": "SQL Server Sku"
      }
    },
    "adminUsername": {
      "type": "String",
      "metadata": {
        "description": "The admin user name of the VM"
      }
    },
    "adminPassword": {
        "type": "SecureString",
        "metadata": {
        "description": "The admin password of the VM"
      }
    },
    "storageWorkloadType": {
      "type": "String",
      "defaultValue": "General",
      "allowedValues": [
        "General",
        "OLTP",
        "DW"
      ],
      "metadata": {
        "description": "SQL Server Workload Type"
      }
    },
    "sqlVirtualMachineName": {
        "type": "string",
        "defaultValue": "ion5eddb999"
    },
    "sqlDataDisksCount": {
      "type": "int",
      "defaultValue": 1,
      "minValue": 1,
      "maxValue": 8,
      "metadata": {
        "description": "Amount of data disks (1TB each) for SQL Data files"
      }
    },
    "dataPath": {
      "type": "String",
      "defaultValue": "F:\\SQLData",
      "metadata": {
        "description": "Path for SQL Data files. Please choose drive letter from F to Z, and other drives from A to E are reserved for system"
      }
    },
    "sqlLogDisksCount": {
      "type": "int",
      "defaultValue": 1,
      "minValue": 1,
      "maxValue": 8,
      "metadata": {
        "description": "Amount of data disks (1TB each) for SQL Log files"
      }
    },
    "logPath": {
      "type": "String",
      "defaultValue": "G:\\SQLLog",
      "metadata": {
        "description": "Path for SQL Log files. Please choose drive letter from F to Z and different than the one used for SQL data. Drive letter from A to E are reserved for system"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "networkInterfaceName": "[concat(parameters('virtualMachineName'), '-nic')]",
    //"networkSecurityGroupName": "[concat(parameters('virtualMachineName'), '-nsg')]",
    "diskConfigurationType": "NEW",
    "subnetRef": "[resourceID(parameters('existingVNetResourceGroup'), 'Microsoft.Network/virtualNetWorks/subnets', parameters('existingVirtualNetworkName'), parameters('existingSubNetName'))]",
    "dataDisksLuns": "[array(range(0 ,parameters('sqlDataDisksCount')))]",
    "logDisksLuns": "[array(range(parameters('sqlDataDisksCount'), parameters('sqlLogDisksCount')))]",
    "dataDisks": {
      "createOption": "empty",
      "caching": "ReadOnly",
      "writeAcceleratorEnabled": false,
      "storageAccountType": "StandardSSD_LRS",
      "diskSizeGB": 100
    },
    "tempDbPath": "D:\\SQLTemp"
  },
  "resources": [
    {
      "type": "Microsoft.Network/networkInterfaces",
      "apiVersion": "2020-06-01",
      "name": "[variables('networkInterfaceName')]",
      "location": "[parameters('location')]",
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "privateIPAllocationMethod": "Static",
              "privateIPAddress": "[parameters('ipAddress')]",
              "privateIPAddressVersion": "IPv4"
            }
          }
        ],
        "enableAcceleratedNetworking": false
        }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2020-06-01",
      "name": "[parameters('virtualMachineName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('virtualMachineSize')]"
        },
        "storageProfile": {
          "osDisk": {
            "createOption": "fromImage",
            "managedDisk": {
              "storageAccountType": "StandardSSD_LRS"
            }
          },
          "imageReference": {
            "publisher": "MicrosoftSQLServer",
            "offer": "[parameters('imageOffer')]",
            "sku": "[parameters('sqlSku')]",
            "version": "latest"
          },
          "copy": [
            {
              "name": "dataDisks",
              "count": "[add(parameters('sqlDataDisksCount'), parameters('sqlLogDisksCount'))]",
              "input": {
                "lun": "[copyIndex('dataDisks')]",
                "createOption": "[variables('dataDisks').createOption]",
                "caching": "[if(greaterOrEquals(copyIndex('dataDisks'), parameters('sqlDataDisksCount')) ,'None', variables('dataDisks').caching )]",
                "writeAcceleratorEnabled": "[variables('dataDisks').writeAcceleratorEnabled]",
                "diskSizeGB": "[variables('dataDisks').diskSizeGB]",
                "managedDisk": {
                  "storageAccountType": "[variables('dataDisks').storageAccountType]"
                }
              }
            }
          ]
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
            }
          ]
        },
        "osProfile": {
          "computerName": "[parameters('virtualMachineName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]",
          "windowsConfiguration": {
            "enableAutomaticUpdates": true,
            "provisionVmAgent": true
          }
        }
      }
    },
    {
      "type": "Microsoft.SqlVirtualMachine/SqlVirtualMachines",
      "apiVersion": "2017-03-01-preview",
      "name": "[last(split(resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName')),'/'))]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
      ],
      "properties": {
        "virtualMachineResourceId": "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
        "sqlManagement": "Full",
        "SqlServerLicenseType": "PAYG",
        "StorageConfigurationSettings": {
          "DiskConfigurationType": "[variables('diskConfigurationType')]",
          "StorageWorkloadType": "[parameters('storageWorkloadType')]",
          "SQLDataSettings": {
            "LUNs": "[variables('dataDisksLUNs')]",
            "DefaultFilePath": "[parameters('dataPath')]"
          },
          "SQLLogSettings": {
            "Luns": "[variables('logDisksLUNs')]",
            "DefaultFilePath": "[parameters('logPath')]"
          },
          "SQLTempDbSettings": {
            "DefaultFilePath": "[variables('tempDbPath')]"
          }
        }
      }
    }
  ],
  "outputs": {
    "virtualMachine": {
        "type": "object",
        "value": "[reference(parameters('virtualMachineName'))]"
    }
  }
}

Solution

  • For your output use:

      "outputs": {
        "virtualMachine": {
            "type": "object",
            "value": "[reference(resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName')))]"
        },
        "sqlVirtualMachine": {
            "type": "object",
            "value": "[reference(resourceId('Microsoft.SqlVirtualMachine/SqlVirtualMachines', parameters('virtualMachineName')))]"
        }
      }
    

    Depending on what you're after... You can name them the same, but any reference to them (dependsOn, reference()) needs to be unambiguous.