5
votes

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')]" }
    }
  }
},
3

3 Answers

12
votes

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`)
5
votes

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')

0
votes

Applying @DeveloperExceptionError answer, here is a bicep example using the aks_enable_private_cluster variable for conditional deployment:

// User Managed Identity
resource idAccessToAks 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = if (aks_enable_private_cluster) {
  name: 'id_aks_access_n'
  location: resourceGroup().location
}

// User Managed Identity RBAC assignment
resource idAccessToAksRoleAssignment 'Microsoft.Authorization/roleAssignments@2020-08-01-preview' = if (aks_enable_private_cluster) {
  name: guid(subscription().id, 'dev', '3498e952-d568-435e-9b2c-8d77e338d7f7')
  properties: {
    principalId: aks_enable_private_cluster ? idAccessToAks.properties.principalId : ''
    roleDefinitionId: '${subscription().id}/providers/Microsoft.Authorization/roleDefinitions/3498e952-d568-435e-9b2c-8d77e338d7f7'
    principalType: 'ServicePrincipal'
  }
}