Search code examples
azureazure-devopsazure-resource-managerazure-rm-template

Arm Template - Why am I getting an error saying resource is not defined in template?


I have a parent arm template that uses various linked component templates. The webApp I am creating requires a dependency on a service plan but after adding a dependency like the one in the dependencies section of the documentation I keep getting an error: 'The resource 'Microsoft.Resources/deployments/NovaArmTestDev' is not defined in the template.

The parent template: (top two deployments are the ones causing issue)

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "resourcegroupName": {
            "type": "string",
            "metadata": {
                "description": "The name given to the group and all resources it contains by default"
            }
        },
        "templateFolderUri": {
            "type": "string",
            "metadata": {
                "description": "The URI of the template component folder"
            }
        }
    },
    "functions": [],
    "variables": {},
    "resources": [
        {
            "name": "[parameters('resourceGroupName')]",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('resourcegroupName')]",
            "apiVersion": "2021-04-01",
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "uri": "[concat(parameters('templateFolderUri'), '/servicePlanCreator.json')]",
                    "contentVersion": "1.0.0.0"
                }
            }
            
        },
        {
            "name": "[concat(parameters('resourceGroupName'), 'App')]",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('resourcegroupName')]",
            "apiVersion": "2021-04-01",
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "uri": "[concat(parameters('templateFolderUri'), '/dualSlotWebApp.json')]",
                    "contentVersion": "1.0.0.0"
                },
                "parameters": {}
            },
            "dependsOn": [
                "[resourceId('Microsoft.Resources/deployments', parameters('resourceGroupName'))]"
            ]
        },
        {
            "name": "[concat(parameters('resourceGroupName'), 'Storage')]",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('resourcegroupName')]",
            "apiVersion": "2021-04-01",
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "uri": "[concat(parameters('templateFolderUri'), '/storageAccountTemplate.json')]",
                    "contentVersion": "1.0.0.0"
                },
                "parameters": {}
            }
        },
        {
            "name": "[concat(parameters('resourceGroupName'), 'Vault')]",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('resourcegroupName')]",
            "apiVersion": "2021-04-01",
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "uri": "[concat(parameters('templateFolderUri'), '/keyVaultCreator.json')]",
                    "contentVersion": "1.0.0.0"
                },
                "parameters": {}
            }
        }
        
    ],
    "outputs": {}
}

servicePlanCreator:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "servicePlanName": {
            "defaultValue": "[resourceGroup().name]",
            "type": "string",
            "metadata": {
                "description": "The name of the newly created resource"
                }
        },
        "operatingSystem": {
            "type": "string",
            "defaultValue": "windows",
            "metadata": {
                "description": "The Operating system the the newly created resource will use"
                }
        },
        "sku": {
            "type": "string",
            "defaultValue": "S1",
            "metadata": {
                "description": "The sku (pricing tier) the resource group the service plan will use"
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().Location]",
            "metadata": {
                "description": "(Optional) The location og the resource. Will default to the location of the resource group if not set."
            }
        }
    },
    "functions": [],
    "variables": {},
    "resources": [
        {
            "name": "[parameters('servicePlanName')]",
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2020-12-01",
            "location": "[parameters('location')]",
            "kind": "[parameters('operatingSystem')]",
            "sku": {
                "name": "[parameters('sku')]"
            },
            "tags": {},
            "properties": {
                "name": "[parameters('servicePlanName')]"
            }
            
        }
    ],
    "outputs": {}
}

dualSlotWebApp template:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "webAppName": {
            "type": "string",
            "defaultValue": "[concat(resourceGroup().name)]",
            "metadata": {
                "description": "(Optional) Web App name. Defaults to '<ResourceGroupName>Plane' if not supplied"
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "(Optional) Web App name. Defaults to Resource group location if not supplied"
            }
        },
        "appServicePlan": {
            "type": "string",
            "defaultValue": "[resourceGroup().name]",
            "metadata": {
                "description": "name of the Service plan the app will be assigned to"
            }
        }
    },
    "functions": [],
    "variables": {},
    "resources": [
        {
            "name": "[parameters('webAppName')]",
            "type": "Microsoft.Web/sites",
            "apiVersion": "2020-12-01",
            "location": "[parameters('location')]",
            "properties": {
                "name": "[parameters('webAppName')]",
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlan'))]"
            },
            "resources": [
                {
                    "name": "[concat(parameters('webAppName'), '/Slot1')]",
                    "type": "Microsoft.Web/sites/slots",
                    "apiVersion": "2021-03-01",
                    "location": "[resourceGroup().location]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/sites', parameters('webAppName'))]"
                    ],
                    "tags": {
                        "displayName": "Web Deploy for webApp1"
                    },
                    "properties": {
                        "packageUri": "[concat('artifactsLocation', '/WebPackages/webApp1.zip', 'artifactsLocationSasToken')]",
                        "dbType": "None",
                        "connectionString": "",
                        "setParameters": {
                            "IIS Web Application Name": "webApp1"
                        }
                    }
                }
            ]
        }
    ],
    "outputs": {}
}

Solution

  • Referencing the templates from your earlier question, it appeared that the dependsOn was not configured correctly.

    Originally, it was setup incorrectly with:

    "[resourceId('Microsoft.Resources/resourceGroups/', parameters('resourceGroupName'))]"
    

    I updated in two places to use the same deployment name:

    "[concat(parameters('resourceGroupName'), 'ServicePlan')]"
    

    The two sections look like:

      "resources": [
    
    ...
    
        {
          "name": "[concat(parameters('resourceGroupName'), 'ServicePlan')]",
          "type": "Microsoft.Resources/deployments",
          "resourceGroup": "[parameters('resourcegroupName')]",
          "apiVersion": "2021-04-01",
          "properties": {
            "mode": "Incremental",
            "templateLink": {
              "uri": "[concat(parameters('templateFolderUri'), '/servicePlanCreator.json')]",
              "contentVersion": "1.0.0.0"
            }
          },
          "dependsOn": [
            "[resourceId('Microsoft.Resources/resourceGroups/', parameters('resourceGroupName'))]"
          ]
        },
    
    ...
    
        {
          "name": "[concat(parameters('resourceGroupName'), 'App')]",
          "type": "Microsoft.Resources/deployments",
          "resourceGroup": "[parameters('resourcegroupName')]",
          "apiVersion": "2021-04-01",
          "properties": {
            "mode": "Incremental",
            "templateLink": {
              "uri": "[concat(parameters('templateFolderUri'), '/dualSlotWebApp.json')]",
              "contentVersion": "1.0.0.0"
            },
            "parameters": {}
          },
          "dependsOn": [
            "[concat(parameters('resourceGroupName'), 'ServicePlan')]"
          ]
        }
      ],
    

    Setting the dependency as I have ensures that the service plan completes deployment before the app begins deployment. enter image description here