Search code examples
azureazure-rm-template

The resource is not defined in the template - Azure ARM


I am trying to create a deployment template that creates a resource group and a VM with the necessary resources within it. The resources in their own deploymentTemplate deploy just fine. But in the subscriptionDeploymentTemplate I get the following error (in validation):

{
  "code": "InvalidTemplate",
  "message": "Deployment template validation failed: 'The resource 'Microsoft.Network/networkInterfaces/Assessment-NetInterfacescrobp34x4564' is not defined in the template. Please see https://aka.ms/arm-template for usage details.'."
}

After a lot of googling I have not found a solution that works in my case. I would appreciate any help.

The template:

    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "rgName": {
            "type": "string"
        }
    },
    "variables": {
        "rgLocation": "westeurope",
        "vm_id": "[concat('AssessmentVM', uniquestring(deployment().name))]",
        "location": "westeurope",
        "vnet_id": "[concat('Assessment-Vnet', uniquestring(deployment().name))]",
        "nic_id": "[concat('Assessment-NetInterface', uniquestring(deployment().name))]",
        "publicIP_id": "[concat('AssessmentVM-ip', uniquestring(deployment().name))]",
        "nsg_id": "[concat('AssessmentVM-nsg', uniquestring(deployment().name))]",
        "vmImage_id": "/subscriptions/x/resourceGroups/AssessmentCase_Snapshot/providers/Microsoft.Compute/galleries/AssessmentVM_Images/images/AssessmentVM/versions/0.0.1"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2021-04-01",
            "name": "[parameters('rgName')]",
            "location": "[variables('rgLocation')]",
            "properties": {}
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2021-04-01",
            "name": "assessmentDeployment",
            "resourceGroup": "[parameters('rgName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.Network/networkSecurityGroups",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('nsg_id')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "securityRules": [
                                    {
                                        "name": "RDP",
                                        "properties": {
                                            "protocol": "TCP",
                                            "sourcePortRange": "*",
                                            "destinationPortRange": "3389",
                                            "sourceAddressPrefix": "*",
                                            "destinationAddressPrefix": "*",
                                            "access": "Allow",
                                            "priority": 300,
                                            "direction": "Inbound",
                                            "sourcePortRanges": [],
                                            "destinationPortRanges": [],
                                            "sourceAddressPrefixes": [],
                                            "destinationAddressPrefixes": []
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "type": "Microsoft.Network/publicIPAddresses",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('publicIP_id')]",
                            "location": "[variables('location')]",
                            "sku": {
                                "name": "Basic"
                            },
                            "properties": {
                                "publicIPAddressVersion": "IPv4",
                                "publicIPAllocationMethod": "Dynamic",
                                "idleTimeoutInMinutes": 4,
                                "ipTags": []
                            }
                        },
                        {
                            "type": "Microsoft.Network/virtualNetworks",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('vnet_id')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "10.1.4.0/24"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "default",
                                        "properties": {
                                            "addressPrefix": "10.1.4.0/24",
                                            "delegations": [],
                                            "privateEndpointNetworkPolicies": "Enabled",
                                            "privateLinkServiceNetworkPolicies": "Enabled"
                                        }
                                    }
                                ],
                                "virtualNetworkPeerings": [],
                                "enableDdosProtection": false
                            }
                        },
                        {
                            "type": "Microsoft.Compute/virtualMachines",
                            "apiVersion": "2019-07-01",
                            "name": "[variables('vm_id')]",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                            ],
                            "properties": {
                                "hardwareProfile": {
                                    "vmSize": "Standard_D4s_v3"
                                },
                                "storageProfile": {
                                    "imageReference": {
                                        "id": "[variables('vmImage_id')]"
                                    },
                                    "osDisk": {
                                        "createOption": "FromImage"
                                    }
                                },
                                "networkProfile": {
                                    "networkInterfaces": [
                                        {
                                            "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                                        }
                                    ]
                                },
                                "diagnosticsProfile": {
                                    "bootDiagnostics": {
                                        "enabled": false
                                    }
                                },
                                "licenseType": "Windows_Client"
                            }
                        },
                        {
                            "type": "Microsoft.Network/networkSecurityGroups/securityRules",
                            "apiVersion": "2020-05-01",
                            "name": "[concat(variables('nsg_id'), '/RDP')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                            ],
                            "properties": {
                                "protocol": "TCP",
                                "sourcePortRange": "*",
                                "destinationPortRange": "3389",
                                "sourceAddressPrefix": "*",
                                "destinationAddressPrefix": "*",
                                "access": "Allow",
                                "priority": 300,
                                "direction": "Inbound",
                                "sourcePortRanges": [],
                                "destinationPortRanges": [],
                                "sourceAddressPrefixes": [],
                                "destinationAddressPrefixes": []
                            }
                        },
                        {
                            "type": "Microsoft.Network/virtualNetworks/subnets",
                            "apiVersion": "2020-05-01",
                            "name": "[concat(variables('vnet_id'), '/default')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/virtualNetworks', variables('vnet_id'))]"
                            ],
                            "properties": {
                                "addressPrefix": "10.1.4.0/24",
                                "delegations": [],
                                "privateEndpointNetworkPolicies": "Enabled",
                                "privateLinkServiceNetworkPolicies": "Enabled"
                            }
                        },
                        {
                            "type": "Microsoft.Network/networkInterfaces",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('nic_id')]",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]",
                                "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]",
                                "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                            ],
                            "properties": {
                                "ipConfigurations": [
                                    {
                                        "name": "ipconfig1",
                                        "properties": {
                                            "privateIPAddress": "10.1.4.4",
                                            "privateIPAllocationMethod": "Dynamic",
                                            "publicIPAddress": {
                                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]"
                                            },
                                            "subnet": {
                                                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]"
                                            },
                                            "primary": true,
                                            "privateIPAddressVersion": "IPv4"
                                        }
                                    }
                                ],
                                "dnsSettings": {
                                    "dnsServers": []
                                },
                                "enableAcceleratedNetworking": false,
                                "enableIPForwarding": false,
                                "networkSecurityGroup": {
                                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                                }
                            }
                        }
                    ]
                }
            }
        }
    ],
    "outputs": {}
}

Solution

  • The problem was the resourceId function which was in the wrong scope and therefore need to be additionally supplied with the subscription id and resource group name to return the correct ids. This template is now working as expected

    {
        "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "rg_name": {
                "type": "string"
            },
            "location": {
                "type": "string",
                "defaultValue": "westeurope"
            }
        },
        "variables": {
            "vm_id": "[concat('AssessmentVM', uniquestring(deployment().name))]",
            "vnet_id": "[concat('Assessment-Vnet', uniquestring(deployment().name))]",
            "nic_id": "[concat('Assessment-NetInterface', uniquestring(deployment().name))]",
            "publicIP_id": "[concat('AssessmentVM-ip', uniquestring(deployment().name))]",
            "nsg_id": "[concat('AssessmentVM-nsg', uniquestring(deployment().name))]",
            "subscription_id": "x",
            "vmImage_id": "/subscriptions/x/resourceGroups/AssessmentCase_Snapshot/providers/Microsoft.Compute/galleries/AssessmentVM_Images/images/AssessmentVM/versions/0.0.1"
        },
        "resources": [
            {
                "type": "Microsoft.Resources/resourceGroups",
                "apiVersion": "2021-04-01",
                "name": "[parameters('rg_name')]",
                "location": "[parameters('location')]",
                "properties": {}
            },
            {
                "type": "Microsoft.Resources/deployments",
                "apiVersion": "2021-04-01",
                "name": "assessment_vm_deployment",
                "resourceGroup": "[parameters('rg_name')]",
                "dependsOn": [
                    "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rg_name'))]"
                ],
                "properties": {
                    "mode": "Incremental",
                    "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {},
                        "variables": {},
                        "resources": [
                            {
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "apiVersion": "2020-05-01",
                                "name": "[variables('nsg_id')]",
                                "location": "[parameters('location')]",
                                "properties": {
                                    "securityRules": [
                                        {
                                            "name": "RDP",
                                            "properties": {
                                                "protocol": "TCP",
                                                "sourcePortRange": "*",
                                                "destinationPortRange": "3389",
                                                "sourceAddressPrefix": "*",
                                                "destinationAddressPrefix": "*",
                                                "access": "Allow",
                                                "priority": 300,
                                                "direction": "Inbound",
                                                "sourcePortRanges": [],
                                                "destinationPortRanges": [],
                                                "sourceAddressPrefixes": [],
                                                "destinationAddressPrefixes": []
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "type": "Microsoft.Network/publicIPAddresses",
                                "apiVersion": "2020-05-01",
                                "name": "[variables('publicIP_id')]",
                                "location": "[parameters('location')]",
                                "sku": {
                                    "name": "Basic"
                                },
                                "properties": {
                                    "publicIPAddressVersion": "IPv4",
                                    "publicIPAllocationMethod": "Dynamic",
                                    "idleTimeoutInMinutes": 4,
                                    "ipTags": []
                                }
                            },
                            {
                                "type": "Microsoft.Network/virtualNetworks",
                                "apiVersion": "2020-05-01",
                                "name": "[variables('vnet_id')]",
                                "location": "[parameters('location')]",
                                "properties": {
                                    "addressSpace": {
                                        "addressPrefixes": [
                                            "10.1.4.0/24"
                                        ]
                                    },
                                    "subnets": [
                                        {
                                            "name": "default",
                                            "properties": {
                                                "addressPrefix": "10.1.4.0/24",
                                                "delegations": [],
                                                "privateEndpointNetworkPolicies": "Enabled",
                                                "privateLinkServiceNetworkPolicies": "Enabled"
                                            }
                                        }
                                    ],
                                    "virtualNetworkPeerings": [],
                                    "enableDdosProtection": false
                                }
                            },
                            {
                                "type": "Microsoft.Compute/virtualMachines",
                                "apiVersion": "2019-07-01",
                                "name": "[variables('vm_id')]",
                                "location": "[parameters('location')]",
                                "dependsOn": [
                                    "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                                ],
                                "properties": {
                                    "hardwareProfile": {
                                        "vmSize": "Standard_D4s_v3"
                                    },
                                    "storageProfile": {
                                        "imageReference": {
                                            "id": "[variables('vmImage_id')]"
                                        },
                                        "osDisk": {
                                            "createOption": "FromImage"
                                        }
                                    },
                                    "networkProfile": {
                                        "networkInterfaces": [
                                            {
                                                "id": "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                                            }
                                        ]
                                    },
                                    "diagnosticsProfile": {
                                        "bootDiagnostics": {
                                            "enabled": false
                                        }
                                    },
                                    "licenseType": "Windows_Client"
                                }
                            },
                            {
                                "type": "Microsoft.Network/virtualNetworks/subnets",
                                "apiVersion": "2020-05-01",
                                "name": "[concat(variables('vnet_id'), '/default')]",
                                "dependsOn": [
                                    "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/virtualNetworks', variables('vnet_id'))]"
                                ],
                                "properties": {
                                    "addressPrefix": "10.1.4.0/24",
                                    "delegations": [],
                                    "privateEndpointNetworkPolicies": "Enabled",
                                    "privateLinkServiceNetworkPolicies": "Enabled"
                                }
                            },
                            {
                                "type": "Microsoft.Network/networkInterfaces",
                                "apiVersion": "2020-05-01",
                                "name": "[variables('nic_id')]",
                                "location": "[parameters('location')]",
                                "dependsOn": [
                                    "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]",
                                    "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]",
                                    "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                                ],
                                "properties": {
                                    "ipConfigurations": [
                                        {
                                            "name": "ipconfig1",
                                            "properties": {
                                                "privateIPAddress": "10.1.4.4",
                                                "privateIPAllocationMethod": "Dynamic",
                                                "publicIPAddress": {
                                                    "id": "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]"
                                                },
                                                "subnet": {
                                                    "id": "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]"
                                                },
                                                "primary": true,
                                                "privateIPAddressVersion": "IPv4"
                                            }
                                        }
                                    ],
                                    "dnsSettings": {
                                        "dnsServers": []
                                    },
                                    "enableAcceleratedNetworking": false,
                                    "enableIPForwarding": false,
                                    "networkSecurityGroup": {
                                        "id": "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        ],
        "outputs": {}
    }