Related
I am looking to create an ARM template code to be able to deploy several VMs with different names using copy and copyindex. To achieve this, I turned to this Microsoft procedure:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageNames": {
"type": "array",
"defaultValue": [
"contoso",
"fabrikam",
"coho"
]
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-04-01",
"name": "[concat(parameters('storageNames')[copyIndex()], uniqueString(resourceGroup().id))]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {},
"copy": {
"name": "storagecopy",
"count": "[length(parameters('storageNames'))]"
}
}
],
"outputs": {}
}
But I have problem when I use array with concat and variables too. it gives me several errors. Can you help me make this code? I would like to use the minimum parameter.
here are my 2 different templates:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"VMNames": {
"type": "array"
},
"Location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"vmSize": {
"type": "string",
"defaultValue": "Standard_B2s"
}
},
"functions": [],
"variables": {
"VnetName": "[concat(resourceGroup().name, '-vnet1')]"
},
"resources": [
{
"name": "[concat(parameters('VMNames')[copyIndex()].name, '-pip01')]",
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"tags": {
"displayName": "PublicIPAddress"
},
"properties": {
"publicIPAllocationMethod": "Dynamic",
"dnsSettings": {
"domainNameLabel": "[toLower(parameters('VMNames')[copyIndex()].name)]"
}
}
},
{
"name": "[concat(parameters('VMNames')[copyIndex()].name, '-nsg01')]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"properties": {
"securityRules": [
{
"name": "nsgRule1",
"properties": {
"description": "description",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "3389",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
}
]
}
},
{
"name": "[variables('VnetName')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkSecurityGroups/', parameters('VMNames')[copyIndex()].name, '-nsg01')]"
],
"tags": {
"displayName": "[parameters('VMNames')[copyIndex()].name]"
},
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.0.0.0/16"
]
},
"subnets": [
{
"name": "[ concat(parameters('VMNames')[copyIndex()].name, '-subnet')]",
"properties": {
"addressPrefix": "10.0.0.0/24",
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('VMNames')[copyIndex()].name, '-nsg01')]"
}
}
}
]
}
},
{
"name": "[concat(parameters('VMNames')[copyIndex()].name, '-nic01')]",
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"dependsOn": [
"[concat('Microsoft.Network/publicIPAddresses/', concat(parameters('VMNames')[copyIndex()].name, '-pip01'))]",
"[concat('Microsoft.Network/virtualNetworks/', variables('VnetName'))]"
],
"tags": {
"displayName": "[parameters('VMNames')[copyIndex()].name]"
},
"properties": {
"ipConfigurations": [
{
"name": "ipConfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(parameters('VMNames')[copyIndex()].name, '-pip01'))]"
},
"subnet": {
"id": "[resourceId( 'Microsoft.Network/virtualNetworks/subnets',variables('VnetName'), concat(parameters('VMNames')[copyIndex()].name, '-subnet'))]"
}
}
}
]
}
},
{
"name": "[parameters('VMNames')[copyIndex()].name]",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-03-01",
"location": "[parameters('Location')]",
"copy": {
"name": "vmcopy",
"count": "[length(parameters('VMNames'))]",
"mode": "Serial"
},
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', concat(parameters('VMNames')[copyIndex()].name, '-nic01'))]"
],
"tags": {
"displayName": "[parameters('VMNames')[copyIndex()].name]"
},
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('VMNames')[copyIndex()].size]"
},
"osProfile": {
"computerName": "[parameters('VMNames')[copyIndex()].name]",
"adminUsername": "cprin",
"adminPassword": "Vivendi$1234"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2012-R2-Datacenter",
"version": "latest"
},
"osDisk": {
"name": "[ concat(parameters('VMNames')[copyIndex()].name, '-OsDisk01')]",
"caching": "ReadWrite",
"createOption": "FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('VMNames')[copyIndex()].name, '-nic01'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true
}
}
}
}
],
"outputs": {}
}
the 2nd different:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"VMNames": {
"type": "array"
},
"Location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"vmSize": {
"type": "string",
"defaultValue": "Standard_B2s"
}
},
"functions": [],
"variables": {
"NSGName": "[concat(parameters('VMNames')[copyIndex()], '-nsg01')]",
"NicName": "[concat(parameters('VMNames')[copyIndex()], '-nic01')]",
"VnetName": "[concat(resourceGroup().name[copyIndex()], '-vnet1')]",
"publicNicName": "[concat(parameters('VMNames')[copyIndex()], '-pip01')]",
"OsDiskName": "[concat(parameters('VMNames')[copyIndex()], '-OsDisk01')]",
"subnetName": "[concat(parameters('VMNames')[copyIndex()], '-subnet')]"
},
"resources": [
{
"name": "[variables('publicNicName')]",
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"tags": {
"displayName": "PublicIPAddress"
},
"properties": {
"publicIPAllocationMethod": "Dynamic",
"dnsSettings": {
"domainNameLabel": "[toLower(parameters('VMNames')[copyIndex()])]"
}
}
},
{
"name": "[variables('NSGName')]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"properties": {
"securityRules": [
{
"name": "nsgRule1",
"properties": {
"description": "description",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "3389",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
}
]
}
},
{
"name": "[variables('VnetName')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkSecurityGroups/', variables('NSGName'))]"
],
"tags": {
"displayName": "[parameters('VMNames')[copyIndex()]]"
},
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.0.0.0/16"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "10.0.0.0/24",
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('NSGName'))]"
}
}
}
]
}
},
{
"name": "[variables('NicName')]",
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"dependsOn": [
"[concat('Microsoft.Network/publicIPAddresses/', variables('publicNicName'))]",
"[concat('Microsoft.Network/virtualNetworks/', variables('VnetName'))]"
],
"tags": {
"displayName": "[parameters('VMNames')[copyIndex()]]"
},
"properties": {
"ipConfigurations": [
{
"name": "ipConfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicNicName'))]"
},
"subnet": {
"id": "[resourceId( 'Microsoft.Network/virtualNetworks/subnets',variables('VnetName'), variables('subnetName'))]"
}
}
}
]
}
},
{
"name": "[parameters('VMNames')[copyIndex()]]",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-03-01",
"location": "[parameters('Location')]",
"copy": {
"name": "vmcopy",
"count": "[length(parameters('VMNames'))]",
"mode": "Serial"
},
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', variables('NicName'))]"
],
"tags": {
"displayName": "[parameters('VMNames')[copyIndex()]]"
},
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"osProfile": {
"computerName": "[parameters('VMNames')[copyIndex()]]",
"adminUsername": "cprin",
"adminPassword": "Vivendi$1234"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2012-R2-Datacenter",
"version": "latest"
},
"osDisk": {
"name": "[variables('OsDiskName')]",
"caching": "ReadWrite",
"createOption": "FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('NicName'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true
}
}
}
}
],
"outputs": {}
}
and here is my parameter file for my 1st code:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"VMNames": {
"value": [
{
"name": "vmfakri",
"size": "Standard_B2s"
},
{
"name": "vmrazane",
"size": "Standard_B2s"
},
{
"name": "vmseif",
"size": "Standard_B2s"
}
]
}
}
}
and here is my parameter file for my 2nd code:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"VMNames": {
"value": [
"vmfakri",
"vmrazane",
"vmseif"
]
}
}
}
Here are the errors encountered:
InvalidTemplate - Deployment template validation failed: 'The template variable 'NSGName' is not valid: The template function 'copyIndex' is not expected at this location. The function can only be used in a resource with copy
specified. Please see https://aka.ms/arm-copy for usage details.. Please see https://aka.ms/arm-template-expressions for usage details.'.
InvalidTemplate - Deployment template validation failed: 'The template resource '[concat(parameters('VMNames'), '-pip01')]' at line '24' and column '9' is not valid: The provided parameters for language function 'concat' are
invalid. Either all or none of the parameters must be an array. Please see https://aka.ms/arm-template-expressions/#concat for usage details.. Please see https://aka.ms/arm-template-expressions for usage details.'.
It seem that you might be missing iteration property in resource deployment:
add "copy" property.
{
"name": "[concat(parameters('VMNames')[copyIndex()].name, '-nsg01')]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"properties": {
"securityRules": [
{
"name": "nsgRule1",
"properties": {
"description": "description",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "3389",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
}
]
},
"copy": {
"name": "VMcopy",
"count": "[length(parameters('VMNames'))]"
}
}
In this case you don't need to iterate in variable
Edited
Here is updated ARM template that was successfully validated.
I removed iteration over most of your variables and added iteration over resource.
I noticed that you wanted to have one VNET with multiple subnets so also added iteration over only "subnet" property
I believe you may modify it further to fit your needs.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"VMNames": {
"type": "array"
},
"Location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"vmSize": {
"type": "string",
"defaultValue": "Standard_B2s"
}
},
"functions": [],
"variables": {
"copy": [
{
"name": "NSGName",
"count": "[length(parameters('VMNames'))]",
"input": {
"name": "[concat(parameters('VMNames')[copyIndex('NSGName')], '-nsg01')]"
}
}
]
},
"resources": [
{
"name": "[concat(parameters('VMNames')[copyIndex()], '-pip01')]",
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"tags": {
"displayName": "PublicIPAddress"
},
"properties": {
"publicIPAllocationMethod": "Dynamic",
"dnsSettings": {
"domainNameLabel": "[toLower(parameters('VMNames')[copyIndex()])]"
}
},
"copy": {
"name": "VMcopy",
"count": "[length(parameters('VMNames'))]"
}
},
{
"name": "[concat(parameters('VMNames')[copyIndex()], '-nsg01')]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"properties": {
"securityRules": [
{
"name": "nsgRule1",
"properties": {
"description": "description",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "3389",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
}
]
},
"copy": {
"name": "VMcopy",
"count": "[length(parameters('VMNames'))]"
}
},
{
"name": "[concat(resourceGroup().name, '-vnet1')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.0.0.0/16"
]
},
"copy": [
{
"name": "subnets",
"count": "[length(parameters('VMNames'))]",
"input": {
"subnets": [
{
"name": "[concat(parameters('VMNames')[copyIndex('subnets')], '-subnet')]",
"properties": {
"addressPrefix": "[concat('10.0.',copyIndex('subnets'), '.0/24')]",
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', concat(parameters('VMNames')[copyIndex('subnets')], '-nsg01'))]"
}
}
}
]
}
}
]
}
},
{
"name": "[concat(parameters('VMNames')[copyIndex()], '-nic01')]",
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2020-11-01",
"location": "[parameters('Location')]",
"dependsOn": [
"[concat('Microsoft.Network/publicIPAddresses/', concat(parameters('VMNames')[copyIndex()], '-pip01'))]",
"[concat('Microsoft.Network/virtualNetworks/', concat(resourceGroup().name, '-vnet1'))]"
],
"tags": {
"displayName": "[parameters('VMNames')[copyIndex()]]"
},
"properties": {
"ipConfigurations": [
{
"name": "ipConfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(parameters('VMNames')[copyIndex()], '-pip01'))]"
},
"subnet": {
"id": "[resourceId( 'Microsoft.Network/virtualNetworks/subnets',concat(resourceGroup().name, '-vnet1'), concat(parameters('VMNames')[copyIndex()], '-subnet'))]"
}
}
}
]
},
"copy": {
"name": "VMcopy",
"count": "[length(parameters('VMNames'))]"
}
},
{
"name": "[parameters('VMNames')[copyIndex()]]",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-03-01",
"location": "[parameters('Location')]",
"copy": {
"name": "VMcopy",
"count": "[length(parameters('VMNames'))]"
},
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', concat(parameters('VMNames')[copyIndex()], '-nic01'))]"
],
"tags": {
"displayName": "[parameters('VMNames')[copyIndex()]]"
},
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"osProfile": {
"computerName": "[parameters('VMNames')[copyIndex()]]",
"adminUsername": "cprin",
"adminPassword": "Vivendi$1234"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2012-R2-Datacenter",
"version": "latest"
},
"osDisk": {
"name": "[concat(parameters('VMNames')[copyIndex()], '-OsDisk01')]",
"caching": "ReadWrite",
"createOption": "FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('VMNames')[copyIndex()], '-nic01'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true
}
}
}
}
],
"outputs": {}
}
and parameter file:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"VMNames": {
"value": [
"vmfakri",
"vmrazane",
"vmseif"
]
}
}
}
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": {}
}
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": {}
}
Please find the below template which I have modified -
There were few resources which were same but you were creating them 2 times, so modified that.
use of dependsOn - This error usually comes from having a dependency on a resource and we are not using dependsOn correctly, so modified the order of the resources that you were creating.
{
"$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": [
{
"apiVersion": "2020-05-01",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIP_id')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAddressVersion": "IPv4",
"publicIPAllocationMethod": "Dynamic",
"idleTimeoutInMinutes": 4,
"ipTags": []
}
},
{
"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/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.Network/networkInterfaces",
"apiVersion": "2020-05-01",
"name": "[variables('nic_id')]",
"location": "[variables('location')]",
"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'))]"
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]",
"[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]",
"[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
]
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2019-07-01",
"name": "[variables('vm_id')]",
"location": "[variables('location')]",
"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"
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', variables('nic_id'))]"
]
}
]
}
}
}
],
"outputs": {}
}
The following Azure Resource Manager Template fails validation, Visual Studio Code indicates:
Template validation failed: Invalid property identifier character: {. Path 'resources[6].identity', line 311, position 8.
Missing member name. arm-template (syntax) [311, 9]
The object is unclosed, '}' expected. arm-template -syntax) [319, 10]
Missing required property "xmlCfg" arm-template (schema) [51, 13]
Missing required property "properties" arm-template (schema) [258, 9]
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"virtualNetworkName": {
"type": "String",
"defaultValue": "webapp101-vnet"
},
"virtualMachineName": {
"type": "String",
"defaultValue": "webapp101-vm01"
},
"virtualMachineSize": {
"type": "String",
"defaultValue": "Standard_DS1_v2"
},
"adminUsername": {
"type": "String",
"defaultValue": "azureuser"
},
"adminPassword": {
"type": "SecureString"
}
},
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'sawinvm')]",
"accountid": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
"vmName": "[parameters('virtualMachineName')]",
"networkSecurityGroupName": "[concat(parameters('virtualNetworkName'), '-nsg')]",
"addressPrefix": "10.1.0.0/16",
"subnetPrefix": "10.1.0.0/24",
"subnetName": "web",
"publicIPAddressName": "[concat(variables('vmName'), '-ip')]",
"nicName": "[concat(variables('vmName'), '-nic')]",
"subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), variables('subnetName'))]"
},
"resources": [
{
"type": "Microsoft.Compute/virtualMachines/extensions",
"name": "[concat(variables('vmName'), '/', 'Microsoft.Insights.VMDiagnosticsSettings')]",
"apiVersion": "2017-12-01",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
],
"properties": {
"publisher": "Microsoft.Azure.Diagnostics",
"type": "IaaSDiagnostics",
"typeHandlerVersion": "1.12",
"autoUpgradeMinorVersion": true,
"settings": {
"WadCfg": {
"DiagnosticMonitorConfiguration": {
"overallQuotaInMB": 4096,
"DiagnosticInfrastructureLogs": {
"scheduledTransferLogLevelFilter": "Error"
},
"Directories": {
"scheduledTransferPeriod": "PT1M",
"IISLogs": {
"containerName": "wad-iis-logfiles"
},
"FailedRequestLogs": {
"containerName": "wad-failedrequestlogs"
}
},
"PerformanceCounters": {
"scheduledTransferPeriod": "PT1M",
"sinks": "AzMonSink",
"PerformanceCounterConfiguration": [
{
"counterSpecifier": "\\Memory\\Available Bytes",
"sampleRate": "PT15S"
},
{
"counterSpecifier": "\\Memory\\% Committed Bytes In Use",
"sampleRate": "PT15S"
},
{
"counterSpecifier": "\\Memory\\Committed Bytes",
"sampleRate": "PT15S"
}
]
},
"WindowsEventLog": {
"scheduledTransferPeriod": "PT1M",
"DataSource": [
{
"name": "Application!*"
}
]
},
"Logs": {
"scheduledTransferPeriod": "PT1M",
"scheduledTransferLogLevelFilter": "Error"
}
},
"SinksConfig": {
"Sink": [
{
"name" : "AzMonSink",
"AzureMonitor" : {}
}
]
}
},
"StorageAccount": "[variables('storageAccountName')]"
},
"protectedSettings": {
"storageAccountName": "[variables('storageAccountName')]",
"storageAccountKey": "[listKeys(variables('accountid'),'2015-06-15').key1]",
"storageAccountEndPoint": "https://core.windows.net/"
}
}
},
{
"type": "Microsoft.Compute/virtualMachines/extensions",
"name": "[concat(variables('vmName'), '/', 'WADExtensionSetup')]",
"apiVersion": "2017-12-01",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]" ],
"properties": {
"publisher": "Microsoft.ManagedIdentity",
"type": "ManagedIdentityExtensionForWindows",
"typeHandlerVersion": "1.0",
"autoUpgradeMinorVersion": true,
"settings": {
"port": 50342
}
}
},
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2018-07-01",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard_LRS",
"tier": "Standard"
},
"kind": "StorageV2",
"properties": {
"networkAcls": {
"bypass": "AzureServices",
"virtualNetworkRules": [],
"ipRules": [],
"defaultAction": "Allow"
},
"supportsHttpsTrafficOnly": false,
"encryption": {
"services": {
"file": {
"enabled": true
},
"blob": {
"enabled": true
}
},
"keySource": "Microsoft.Storage"
}
},
"dependsOn": []
},
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "[variables('networkSecurityGroupName')]",
"apiVersion": "2018-08-01",
"location": "[resourceGroup().location]",
"tags": {},
"properties": {
"securityRules": [
{
"name": "HTTP",
"properties": {
"priority": 300,
"protocol": "TCP",
"access": "Allow",
"direction": "Inbound",
"sourceAddressPrefix": "*",
"sourcePortRange": "*",
"destinationAddressPrefix": "*",
"destinationPortRange": "80"
}
},
{
"name": "RDP",
"properties": {
"priority": 320,
"protocol": "TCP",
"access": "Allow",
"direction": "Inbound",
"sourceAddressPrefix": "*",
"sourcePortRange": "*",
"destinationAddressPrefix": "*",
"destinationPortRange": "3389"
}
}
]
},
"dependsOn": []
},
{
"type": "Microsoft.Network/virtualNetworks",
"name": "[parameters('virtualNetworkName')]",
"apiVersion": "2018-08-01",
"location": "[resourceGroup().location]",
"tags": {},
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]",
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
}
}
}
]
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
]
},
{
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"apiVersion": "2018-04-01",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Network/networkSecurityGroups/', variables('networkSecurityGroupName'))]",
"[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
"[concat('Microsoft.Network/publicIpAddresses/', variables('publicIpAddressName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIpAddress": {
"id": "[resourceId('Microsoft.Network/publicIpAddresses', variables('publicIpAddressName'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
]
}
},
{
"apiVersion": "2017-03-30",
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('vmName')]",
"location": "[resourceGroup().location]",
"identity": {
"type": "SystemAssigned",
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('virtualMachineSize')]"
},
"storageProfile": {
"osDisk": {
"createOption": "fromImage",
"managedDisk": {
"storageAccountType": null
}
},
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2019-Datacenter",
"version": "latest"
}
},
"osProfile": {
"computerName": "[variables('vmName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]",
"windowsConfiguration": {
"enableAutomaticUpdates": true,
"provisionVmAgent": true
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob]"
}
}
}
},
{
"name": "[variables('publicIpAddressName')]",
"type": "Microsoft.Network/publicIpAddresses",
"apiVersion": "2019-04-01",
"location": "[resourceGroup().location]",
"properties": {
"publicIpAllocationMethod": null
}
}
]
}
There is a missing bracket for the "identity" property on line 263.
Correct code:
"apiVersion": "2017-03-30",
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('vmName')]",
"location": "[resourceGroup().location]",
"identity": {
"type": "SystemAssigned"}, //**Missing bracket**
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts/',
variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces/',
variables('nicName'))]"
],
I downloaded ARM template from Azure for deploying VM. I did not modify the script in any way. When I run the deploy.ps1 script I get the following error
New-AzureRmResourceGroupDeployment : 1:05:56 PM - Resource Microsoft.Network/networkInterfaces 'win2016vm293' failed
with message '{
"error": {
"code": "InvalidResourceReference",
"message": "Resource /subscriptions/<subscriptionId>/resourceGroups/win2016vm2/providers/Micros
oft.Network/networkSecurityGroups/Win2016vm2-nsg referenced by resource /subscriptions/<subscriptionId>/resourceGroups/Win2016FromScript/providers/Microsoft.Network/networkInterfaces/win2016vm293 was not found.
Please make sure that the referenced resource exists, and that both resources are in the same region.",
"details": []
}
}'
The following is the template.js file.
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"virtualMachineName": {
"type": "string"
},
"virtualMachineSize": {
"type": "string"
},
"adminUsername": {
"type": "string"
},
"virtualNetworkName": {
"type": "string"
},
"networkInterfaceName": {
"type": "string"
},
"networkSecurityGroupName": {
"type": "string"
},
"adminPassword": {
"type": "securestring"
},
"addressPrefix": {
"type": "string"
},
"subnetName": {
"type": "string"
},
"subnetPrefix": {
"type": "string"
},
"publicIpAddressName": {
"type": "string"
},
"publicIpAddressType": {
"type": "string"
},
"publicIpAddressSku": {
"type": "string"
},
"autoShutdownStatus": {
"type": "string"
},
"autoShutdownTime": {
"type": "string"
},
"autoShutdownTimeZone": {
"type": "string"
},
"autoShutdownNotificationStatus": {
"type": "string"
}
},
"variables": {
"vnetId": "[resourceId('win2016vm2','Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
"subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
},
"resources": [
{
"name": "[parameters('virtualMachineName')]",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2018-04-01",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
],
"properties": {
"osProfile": {
"computerName": "[parameters('virtualMachineName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]",
"windowsConfiguration": {
"provisionVmAgent": "true"
}
},
"hardwareProfile": {
"vmSize": "[parameters('virtualMachineSize')]"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2016-Datacenter",
"version": "latest"
},
"osDisk": {
"createOption": "fromImage",
"managedDisk": {
"storageAccountType": "Premium_LRS"
}
},
"dataDisks": []
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
}
]
}
}
},
{
"name": "[concat('shutdown-computevm-', parameters('virtualMachineName'))]",
"type": "Microsoft.DevTestLab/schedules",
"apiVersion": "2017-04-26-preview",
"location": "[parameters('location')]",
"properties": {
"status": "[parameters('autoShutdownStatus')]",
"taskType": "ComputeVmShutdownTask",
"dailyRecurrence": {
"time": "[parameters('autoShutdownTime')]"
},
"timeZoneId": "[parameters('autoShutdownTimeZone')]",
"targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
"notificationSettings": {
"status": "[parameters('autoShutdownNotificationStatus')]",
"timeInMinutes": "30"
}
},
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]"
]
},
{
"name": "[parameters('virtualNetworkName')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-02-01",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[parameters('addressPrefix')]"
]
},
"subnets": [
{
"name": "[parameters('subnetName')]",
"properties": {
"addressPrefix": "[parameters('subnetPrefix')]"
}
}
]
}
},
{
"name": "[parameters('networkInterfaceName')]",
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2018-04-01",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
"[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]",
"[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "[variables('subnetRef')]"
},
"privateIPAllocationMethod": "Dynamic",
"publicIpAddress": {
"id": "[resourceId('win2016vm2','Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
}
}
}
],
"enableAcceleratedNetworking": true,
"networkSecurityGroup": {
"id": "[resourceId('win2016vm2', 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
}
}
},
{
"name": "[parameters('publicIpAddressName')]",
"type": "Microsoft.Network/publicIpAddresses",
"apiVersion": "2017-08-01",
"location": "[parameters('location')]",
"properties": {
"publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
},
"sku": {
"name": "[parameters('publicIpAddressSku')]"
}
},
{
"name": "[parameters('networkSecurityGroupName')]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2018-01-01",
"location": "[parameters('location')]",
"properties": {
"securityRules": [
{
"name": "RDP",
"properties": {
"priority": 300,
"protocol": "TCP",
"access": "Allow",
"direction": "Inbound",
"sourceApplicationSecurityGroups": [],
"destinationApplicationSecurityGroups": [],
"sourceAddressPrefix": "*",
"sourcePortRange": "*",
"destinationAddressPrefix": "*",
"destinationPortRange": "3389"
}
}
]
}
}
],
"outputs": {
"adminUsername": {
"type": "string",
"value": "[parameters('adminUsername')]"
}
}
}
After running the script I see that only Win2016vm2-nsg (Network security group), as well as the IP and the VNet resources are created.
In the template, the section for creating Microsoft.Network/networkInterfaces contains dependsOn section with networkSecurityGroups resource name in it, which seems correct. So I'm not sure what is wrong here.
Your template is using hardcoded resourceGroup name for several resources, you need to amend that, change 'win2016vm2' to a resourceGroup you are deploying to, or remove resourceGroup from resourceId() function altogether.
I can create a Azure VM with a specific VHD from the template below but how do I also add it to an Availability Set. I can't do this after VM creation so I need to do it here.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"metadata": {
"description": "Location to create the VM in"
}
},
"osDiskVhdUri": {
"type": "string",
"metadata": {
"description": "Uri of the existing VHD"
}
},
"osType": {
"type": "string",
"allowedValues": [
"Windows",
"Linux"
],
"metadata": {
"description": "Type of OS on the existing vhd"
}
},
"vmSize": {
"type": "string",
"defaultValue": "Standard_D2",
"metadata": {
"description": "Size of the VM"
}
},
"vmName": {
"type": "string",
"metadata": {
"description": "Name of the VM"
}
}
},
"variables": {
"api-version": "2015-06-15",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"publicIPAddressName": "specializedVMPublicIP",
"publicIPAddressType": "Dynamic",
"virtualNetworkName": "specializedVMVNET",
"vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
"subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]",
"nicName": "specializedVMNic"
},
"resources": [
{
"apiVersion": "[variables('api-version')]",
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
}
]
}
},
{
"apiVersion": "[variables('api-version')]",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
"[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
]
}
},
{
"apiVersion": "[variables('api-version')]",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "[variables('publicIPAddressType')]"
}
},
{
"apiVersion": "[variables('api-version')]",
"type": "Microsoft.Compute/virtualMachines",
"name": "[parameters('vmName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"storageProfile": {
"osDisk": {
"name": "[concat(parameters('vmName'),'-osDisk')]",
"osType": "[parameters('osType')]",
"caching": "ReadWrite",
"vhd": {
"uri": "[parameters('osDiskVhdUri')]"
},
"createOption": "Attach"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
}
]
}
}
}
]
}
The best way to figure things like this out is to poke through the templates until you find what you need!
So according to this template, you create an Availability Set like this
"resources": [
{
"type": "Microsoft.Compute/availabilitySets",
"name": "availabilitySet1",
"apiVersion": "2015-06-15",
"location": "[parameters('location')]",
"properties": {
"platformFaultDomainCount": "3",
"platformUpdateDomainCount": "20"
}
}
]
and then (according to this) you use it like this
"apiVersion": "[variables('apiVersion')]",
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat('myvm', copyIndex())]",
"location": "[variables('location')]",
"copy": {
"name": "virtualMachineLoop",
"count": "[parameters('numberOfInstances')]"
},
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', 'nic', copyindex())]",
"[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]"
],
"properties": {
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets', variables('availabilitySetName'))]"
},
You would need to be looking at the Microsoft.Compute/availabilitySets resource provider, here is some sample JSON from one of my templates.
"resources": [
{
"type": "Microsoft.Compute/availabilitySets",
"name": "availabilitySet1",
"apiVersion": "2015-06-15",
"location": "[parameters('location')]",
"properties": {
"platformFaultDomainCount": "3",
"platformUpdateDomainCount": "20"
}
}
]
You then need to use the availabilitySet property of the virtualMachines resource provider to add the VMs to the availability set. Make sure you use dependsOn to ensure the availability set is created before the VM. As an example if you refering to it by name:
"properties": {
"hardwareProfile": { "vmSize": "Standard_A0" },
"networkProfile": ...,
"availabilitySet": { "id": "availabilitySet1" },
}