0
votes

I'm trying to get started working with Azure RM templates and having some issues when trying to deploy templates exported from a resource group in the Azure portal.

When I try and deploy my exported template I get the following error:

{"telemetryId":"5d4b90de-18fd-432f-a6c7-cfa65eb22bc8","galleryItemId":"MyGalleryItem","createBlade":"DeployToAzure","code":"InvalidTemplate","message":"Deployment template validation failed: 'The template resource 'myVM-nic' at line '1' and column '3020' is not valid: Unable to evaluate template language function 'resourceId': function requires exactly one multi-segmented argument which must be resource type including resource provider namespace. Current function arguments 'Microsoft.Network/virtualNetworks/subnets,vnet-myVnet/Subnet'. Please see https://aka.ms/arm-template-expressions/#resourceid for usage details.. Please see https://aka.ms/arm-template-expressions for usage details.'."}

Below is a copy of the template as generated by Azure:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "disks_myVM_OsDisk_1_35091da3da6d4b0098638ba9569de541_name": {
        "defaultValue": "myVM_OsDisk_1_35091da3da6d4b0098638ba9569de541",
        "type": "String"
    },
    "virtualMachines_myVM_name": {
        "defaultValue": "myVM",
        "type": "String"
    },
    "networkInterfaces_myVM_nic_name": {
        "defaultValue": "myVM-nic",
        "type": "String"
    },
    "virtualNetworks_vnet_myVnet_name": {
        "defaultValue": "vnet-myVnet",
        "type": "String"
    },
    "subnets_Subnet_name": {
        "defaultValue": "vnet-myVnet/Subnet",
        "type": "String"
    }
},
"variables": {},
"resources": [
    {
        "comments": "Generalized from resource: '/subscriptions/293f1da7-6528-4a0a-a699-afea0f33f605/resourceGroups/simplelinuxvmtest/providers/Microsoft.Compute/disks/myVM_OsDisk_1_35091da3da6d4b0098638ba9569de541'.",
        "type": "Microsoft.Compute/disks",
        "name": "[parameters('disks_myVM_OsDisk_1_35091da3da6d4b0098638ba9569de541_name')]",
        "apiVersion": "2016-04-30-preview",
        "location": "australiaeast",
        "tags": {
            "Role": "Web"
        },
        "scale": null,
        "properties": {
            "accountType": "Standard_LRS",
            "osType": "Linux",
            "creationData": {
                "createOption": "FromImage",
                "imageReference": {
                    "id": "/Subscriptions/293f1da7-6528-4a0a-a699-afea0f33f605/Providers/Microsoft.Compute/Locations/australiaeast/Publishers/Canonical/ArtifactTypes/VMImage/Offers/UbuntuServer/Skus/14.04.5-LTS/Versions/latest"
                }
            },
            "diskSizeGB": 30
        },
        "dependsOn": []
    },
    {
        "comments": "Generalized from resource: '/subscriptions/293f1da7-6528-4a0a-a699-afea0f33f605/resourceGroups/simplelinuxvmtest/providers/Microsoft.Compute/virtualMachines/myVM'.",
        "type": "Microsoft.Compute/virtualMachines",
        "name": "[parameters('virtualMachines_myVM_name')]",
        "apiVersion": "2016-04-30-preview",
        "location": "australiaeast",
        "tags": {
            "Role": "Web"
        },
        "scale": null,
        "properties": {
            "hardwareProfile": {
                "vmSize": "Standard_A1"
            },
            "storageProfile": {
                "imageReference": {
                    "publisher": "Canonical",
                    "offer": "UbuntuServer",
                    "sku": "14.04.5-LTS",
                    "version": "latest"
                },
                "osDisk": {
                    "osType": "Linux",
                    "name": "[concat(parameters('virtualMachines_myVM_name'),'_OsDisk_1_35091da3da6d4b0098638ba9569de541')]",
                    "createOption": "FromImage",
                    "caching": "ReadWrite",
                    "managedDisk": {
                        "storageAccountType": "Standard_LRS",
                        "id": "[resourceId('Microsoft.Compute/disks', parameters('disks_myVM_OsDisk_1_35091da3da6d4b0098638ba9569de541_name'))]"
                    },
                    "diskSizeGB": 30
                },
                "dataDisks": []
            },
            "osProfile": {
                "computerName": "[parameters('virtualMachines_myVM_name')]",
                "adminUsername": "simplelinuxvmtest",
                "linuxConfiguration": {
                    "disablePasswordAuthentication": false
                },
                "secrets": []
            },
            "networkProfile": {
                "networkInterfaces": [
                    {
                        "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaces_myVM_nic_name'))]"
                    }
                ]
            }
        },
        "dependsOn": [
            "[resourceId('Microsoft.Compute/disks', parameters('disks_myVM_OsDisk_1_35091da3da6d4b0098638ba9569de541_name'))]",
            "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaces_myVM_nic_name'))]"
        ]
    },
    {
        "comments": "Generalized from resource: '/subscriptions/293f1da7-6528-4a0a-a699-afea0f33f605/resourceGroups/simplelinuxvmtest/providers/Microsoft.Network/networkInterfaces/myVM-nic'.",
        "type": "Microsoft.Network/networkInterfaces",
        "name": "[parameters('networkInterfaces_myVM_nic_name')]",
        "apiVersion": "2017-06-01",
        "location": "australiaeast",
        "tags": {
            "Role": "Web Server"
        },
        "scale": null,
        "properties": {
            "provisioningState": "Succeeded",
            "resourceGuid": "2ed4e85c-c451-4eeb-9ca3-6d7f4e97288e",
            "ipConfigurations": [
                {
                    "name": "ipconfig1",
                    "etag": "W/\"53f452fd-21b6-42e9-9b61-84123c4ede84\"",
                    "properties": {
                        "provisioningState": "Succeeded",
                        "privateIPAddress": "10.0.0.4",
                        "privateIPAllocationMethod": "Static",
                        "subnet": {
                            "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('subnets_Subnet_name'))]"
                        },
                        "primary": true,
                        "privateIPAddressVersion": "IPv4"
                    }
                }
            ],
            "dnsSettings": {
                "dnsServers": [],
                "appliedDnsServers": [],
                "internalDomainNameSuffix": "3f5fso43uj3ehpjjv2hnsf0ynf.px.internal.cloudapp.net"
            },
            "macAddress": "00-0D-3A-D1-47-FC",
            "enableAcceleratedNetworking": false,
            "enableIPForwarding": false,
            "primary": true,
            "virtualMachine": {
                "id": "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachines_myVM_name'))]"
            }
        },
        "dependsOn": [
            "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('subnets_Subnet_name'))]",
            "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachines_myVM_name'))]"
        ]
    },
    {
        "comments": "Generalized from resource: '/subscriptions/293f1da7-6528-4a0a-a699-afea0f33f605/resourceGroups/simplelinuxvmtest/providers/Microsoft.Network/virtualNetworks/vnet-myVnet'.",
        "type": "Microsoft.Network/virtualNetworks",
        "name": "[parameters('virtualNetworks_vnet_myVnet_name')]",
        "apiVersion": "2017-06-01",
        "location": "australiaeast",
        "scale": null,
        "properties": {
            "provisioningState": "Succeeded",
            "resourceGuid": "3b597ee9-a2dd-437a-bd29-af0ed917586d",
            "addressSpace": {
                "addressPrefixes": [
                    "10.0.0.0/16"
                ]
            },
            "subnets": [
                {
                    "name": "Subnet",
                    "etag": "W/\"733f320c-fce1-4a70-9a2c-166f61f0b543\"",
                    "properties": {
                        "provisioningState": "Succeeded",
                        "addressPrefix": "10.0.0.0/24"
                    }
                }
            ],
            "virtualNetworkPeerings": []
        },
        "dependsOn": []
    },
    {
        "comments": "Generalized from resource: '/subscriptions/293f1da7-6528-4a0a-a699-afea0f33f605/resourceGroups/simplelinuxvmtest/providers/Microsoft.Network/virtualNetworks/vnet-myVnet/subnets/Subnet'.",
        "type": "Microsoft.Network/virtualNetworks/subnets",
        "name": "[parameters('subnets_Subnet_name')]",
        "apiVersion": "2017-06-01",
        "scale": null,
        "properties": {
            "provisioningState": "Succeeded",
            "addressPrefix": "10.0.0.0/24"
        },
        "dependsOn": [
            "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworks_vnet_myVnet_name'))]"
        ]
    }
]

}

What am I doing wrong? Whats the point of ARM templates if you cant export a working template from an existing resource group?

1

1 Answers

2
votes

You problem lies here:

"subnet": {
     "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('subnets_Subnet_name'))]"
},

the error clearly states that

'resourceId': function requires exactly one multi-segmented argument which must be resource type including resource provider namespace. Current function arguments 'Microsoft.Network/virtualNetworks/subnets,vnet-myVnet/Subnet'.

So you must either redo your template to not include the subnet as separate resource, or create a variable that would extract proper vnet\subnet name from the parameters('subnets_Subnet_name') and pass each to the resourceId function, so it looks like this

"[resourceId(`Microsoft.Network/virtualNetworks/subnets, 'vnet-myVnet', 'Subnet')]"

add a variable like this:

variables: {
    ...
    "subnetSplit": "[split(parameters('subnets_subnet_name))]"
    ...
}

reference it like this:

"subnet" {
    "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('subnetSplit')[0], variables('subnetSplit')[1])]"
}