Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference to 'Microsoft.Resources/deployments/' requires an API version

Tags:

azure

If I add a condition before deploying a template for a virtual network I always get this error :If I remove the condition it works???

template deployment returned the following errors: Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template resource 'Microsoft.Resources/deployments/dm5DbServer' reference to 'Microsoft.Resources/deployments/dm5VirtualNetwork' requires an API version.

"resources": [
{
  "condition": "[equals(parameters('BuildDatabaseServer'), 'yes')]",
  "apiVersion": "2016-02-01",
  "name": "[variables('virtualNetworkName')]",
  "type": "Microsoft.Resources/deployments",
  "dependsOn": [],
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "[concat(parameters('_artifactsLocation'), '/', variables('virtualNetworkTemplateFileName'), parameters('_artifactsLocationSasToken'))]",
      "contentVersion": "1.0.0.0"
    },
    "parameters": {
      "virtualNetworkName": { "value": "[variables('virtualNetworkName')]" },
      "vNetPrefix": { "value": "[variables('vNetPrefix')]" },
      "databaseSubnetPrimaryName": { "value": "[variables('databaseSubnetPrimaryName')]" },
      "databaseSubnetPrimaryPrefix": { "value": "[variables('databaseSubnetPrimaryPrefix')]" },
      "databaseSubnetPrimaryNsgName": { "value": "[variables('databaseSubnetPrimaryNsgName')]" }
    }
  }
},

Template being called:

  {
  "name": "[parameters('virtualNetworkName')]",
  "type": "Microsoft.Network/virtualNetworks",
  "location": "[resourceGroup().location]",
  "apiVersion": "2016-03-30",
  "dependsOn": [
    "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('databaseSubnetPrimaryNsgName'))]",
  ],
  "tags": {
    "displayName": "[parameters('virtualNetworkName')]"
  },
  "properties": {
    "addressSpace": {
      "addressPrefixes": [
        "[parameters('vNetPrefix')]"
      ]
    },
    "subnets": [

      {
        "name": "[parameters('databaseSubnetPrimaryName')]",
        "properties": {
          "addressPrefix": "[parameters('databaseSubnetPrimaryPrefix')]",
          "networkSecurityGroup": {
            "id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('databaseSubnetPrimaryNsgName'))]"
          }
        }
      }

  {
  "condition": "[equals(parameters('BuildDatabaseServer'), 'yes')]",
  "apiVersion": "2016-02-01",
  "name": "[variables('databaseServerName')]",
  "type": "Microsoft.Resources/deployments",
  "dependsOn": [
    "[resourceId('Microsoft.Resources/deployments', variables('virtualNetworkName'))]"
  ],
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "[concat(parameters('_artifactsLocation'), '/', variables('commonTemplateArchiveFolder'), '/', variables('virtualMachineTemplateFileName'), parameters('_artifactsLocationSasToken'))]",
      "contentVersion": "1.0.0.0"
    },
    "parameters": {
      "serverName": { "value": "[variables('databaseServerName')]" },
      "adminUserName": { "value": "[variables('databaseServerAdminUserName')]" },
      "adminPassword": { "value": "[parameters('databaseServerAdminPassword')]" },
      "serverWindowsOSVersion": { "value": "[parameters('databaseServerWindowsOSVersion')]" },
      "serverVmSize": { "value": "[variables('databaseServerVmSize')]" },
      "primaryNetworkSecurityGroupName": { "value": "[variables('databaseSubnetPrimaryNsgName')]" },
      "primarySubnetRef": { "value": "[reference(variables('virtualNetworkName')).outputs.databaseSubnetPrimaryRef.value]" },
      "primaryPrivateIPAddress": { "value": "[variables('databaseServerPrimaryPrivateIPAddress')]" },
      "serverOsDiskStorageAccountType": { "value": "[variables('databaseServerOSDiskStorageAccountType')]" },
      "serverDataDiskStorageAccountType": { "value": "[variables('databaseServerDataDiskStorageAccountType')]" },
      "serverDataDiskSizeGB": { "value": "[variables('databaseServerDataDiskSizeGB')]" },
      "monitoringAgentWorkspaceID": { "value": "[parameters('monitoringAgentWorkspaceID')]" },
      "monitoringAgentWorkspaceKey": { "value": "[parameters('monitoringAgentWorkspaceKey')]" },
      "customscripts": { "value": "[variables('customScripts')]" }
    }
  }
},
like image 467
itye1970 Avatar asked Feb 09 '18 11:02

itye1970


2 Answers

Ok so, judging by the error, you have another child deployment (Microsoft.Resources/deployments/dm5DbServer) in the same template and you are using a reference function to grab some data from that and it fails, because you are not providing the API versión it fails. Check the docs on this. If the resource you are referencing isnt being deployed in the same template you need to provide an api-versión to the reference function.

reference(xxx, '2017-01-01`)
like image 193
4c74356b41 Avatar answered Nov 13 '22 12:11

4c74356b41


In my case I was deploying a resource with a condition. The deployment would fail with said error message when this condition was not met.

The reason for that was that I references the conditional resource somewhere else using the reference(...) statement. It seems that the reference calls are still evaluated even if the resource should not be deployed.

Adding a condition before the reference call fixed the issue:

if({condition}, {original Statement}, 'resource not deployed')

like image 33
DeveloperExceptionError Avatar answered Nov 13 '22 14:11

DeveloperExceptionError