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": {}
}
I'm trying to create a windows Azure VM Scale Set that auto provisions a formatted attached data disk using the MS guide here: https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-attached-disks
No matter what I do I see to get this error:
9:14:16 PM - The deployment 'testvmss' failed with error(s). Showing
1 out of 1 error(s). Status
| Message: VM has reported a failure when processing extension 'customScript'. Error message: "Invalid
| Configuration - CommandToExecute is not specified in the configuration; it must be specified in either
| the protected or public configuration section" More information on troubleshooting is available at
| https://aka.ms/VMExtensionCSEWindowsTroubleshoot (Code:VMExtensionProvisioningError) CorrelationId:
| 3294f49a-23f0-4634-aba0-3bb0e814659e
I've tried:
moving the "commandToExecute" into the protected config area
"typeHandlerVersion" numbers
moving commandToExecute outside of the "settings" section
using case corrected "CommandToExecute"
Simplifying the powershell statement to "powershell echo test"
Searching google for the error message, better/different examples, etc.
Here is the specific section of the ARM:
"extensionProfile": {
"extensions": [
{
"name": "customScript",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.8",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": ["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.ps1"],
"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File prepare_vm_disks.ps1"
}
}
}
]
}
And for reference, here is the full ARM:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"virtualMachineScaleSetName": {
"type": "string"
},
"virtualMachineScaleSetRG": {
"type": "string"
},
"singlePlacementGroup": {
"type": "string"
},
"instanceSize": {
"type": "string"
},
"instanceCount": {
"type": "string"
},
"upgradeMode": {
"type": "string"
},
"priority": {
"type": "string"
},
"enableAcceleratedNetworking": {
"type": "string"
},
"subnetId": {
"type": "string"
},
"osDiskType": {
"type": "string"
},
"dataDisks": {
"type": "array"
},
"addressPrefixes": {
"type": "array"
},
"subnets": {
"type": "array"
},
"virtualNetworkId": {
"type": "string"
},
"virtualNetworkName": {
"type": "string"
},
"networkSecurityGroups": {
"type": "array"
},
"networkInterfaceConfigurations": {
"type": "array"
},
"vmName": {
"type": "string"
},
"scaleInPolicy": {
"type": "object"
},
"overprovision": {
"type": "bool"
},
"upgradePolicy": {
"type": "string"
},
"adminUsername": {
"type": "string"
},
"adminPassword": {
"type": "secureString"
},
"platformFaultDomainCount": {
"type": "string"
}
},
"variables": {
"storageApiVersion": "2019-04-01",
"namingInfix": "[toLower(substring(concat(parameters('virtualMachineScaleSetName'), uniqueString(resourceGroup().id)), 0, 9))]"
},
"resources": [
{
"name": "[parameters('virtualNetworkName')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2019-09-01",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": "[parameters('addressPrefixes')]"
},
"subnets": "[parameters('subnets')]"
}
},
{
"name": "[parameters('networkSecurityGroups')[copyIndex()].name]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2019-02-01",
"location": "[parameters('location')]",
"properties": {
"securityRules": "[parameters('networkSecurityGroups')[copyIndex()].rules]"
},
"copy": {
"name": "networkSecurityGroups",
"count": "[length(parameters('networkSecurityGroups'))]"
}
},
{
"name": "[parameters('virtualMachineScaleSetName')]",
"type": "Microsoft.Compute/virtualMachineScaleSets",
"apiVersion": "2019-12-01",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
"networkSecurityGroups",
"[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]"
],
"sku": {
"name": "[parameters('instanceSize')]",
"capacity": "[int(parameters('instanceCount'))]"
},
"properties": {
"overprovision": "[parameters('overprovision')]",
"upgradePolicy": {
"mode": "[parameters('upgradePolicy')]"
},
"singlePlacementGroup": "[parameters('singlePlacementGroup')]",
"virtualMachineProfile": {
"storageProfile": {
"osDisk": {
"createOption": "fromImage",
"caching": "ReadWrite",
"managedDisk": {
"storageAccountType": "[parameters('osDiskType')]"
}
},
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2016-Datacenter",
"version": "latest"
},
"copy": [
{
"name": "dataDisks",
"count": "[length(parameters('dataDisks'))]",
"input": {
"lun": "[parameters('dataDisks')[copyIndex('dataDisks')].lun]",
"createOption": "[parameters('dataDisks')[copyIndex('dataDisks')].createOption]",
"caching": "[parameters('dataDisks')[copyIndex('dataDisks')].caching]",
"writeAcceleratorEnabled": "[parameters('dataDisks')[copyIndex('dataDisks')].writeAcceleratorEnabled]",
"diskSizeGB": "[parameters('dataDisks')[copyIndex('dataDisks')].diskSizeGB]",
"managedDisk": {
"storageAccountType": "[parameters('dataDisks')[copyIndex('dataDisks')].storageAccountType]",
"diskEncryptionSet": "[parameters('dataDisks')[copyIndex('dataDisks')].diskEncryptionSet]"
},
"diskIOPSReadWrite": "[if(equals( parameters('dataDisks')[copyIndex('dataDisks')].diskIOPSReadWrite, -1), json('null'),parameters('dataDisks')[copyIndex('dataDisks')].diskIOPSReadWrite)]",
"diskMBpsReadWrite": "[if(equals( parameters('dataDisks')[copyIndex('dataDisks')].diskMBpsReadWrite, -1), json('null'),parameters('dataDisks')[copyIndex('dataDisks')].diskMBpsReadWrite)]"
}
}
]
},
"priority": "[parameters('priority')]",
"networkProfile": {
"copy": [
{
"name": "networkInterfaceConfigurations",
"count": "[length(parameters('networkInterfaceConfigurations'))]",
"input": {
"name": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].name]",
"properties": {
"primary": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].primary]",
"enableAcceleratedNetworking": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].enableAcceleratedNetworking]",
"ipConfigurations": [
{
"name": "[concat(parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].name, '-defaultIpConfiguration')]",
"properties": {
"subnet": {
"id": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].subnetId]"
},
"primary": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].primary]",
"applicationGatewayBackendAddressPools": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].applicationGatewayBackendAddressPools]",
"loadBalancerBackendAddressPools": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].loadBalancerBackendAddressPools]",
"loadBalancerInboundNatPools": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].loadBalancerInboundNatPools]",
"publicIPAddressConfiguration": "[if( equals( parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].pipName, ''), json('null'), union(json(concat('{\"name\": \"', parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].pipName, '\"}'))\n ,json('{\"properties\": { \"idleTimeoutInMinutes\": 15}}')))]"
}
}
],
"networkSecurityGroup": "[if( equals( parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].nsgId, ''), json('null'),json(concat('{\"id\": \"', parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].nsgId, '\"}')))]"
}
}
}
]
},
"extensionProfile": {
"extensions": [
{
"name": "customScript",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.8",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": ["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.ps1"],
"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File prepare_vm_disks.ps1"
}
}
}
]
},
"osProfile": {
"computerNamePrefix": "[variables('namingInfix')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]",
"windowsConfiguration": {
"provisionVmAgent": true
}
}
},
"scaleInPolicy": "[parameters('scaleInPolicy')]",
"platformFaultDomainCount": "[parameters('platformFaultDomainCount')]"
}
}
],
"outputs": {
"adminUsername": {
"type": "string",
"value": "[parameters('adminUsername')]"
}
}
}
Any help is greatly appreciated, thanks!
I had the same issue with VMSS Managed Disk and Service Fabric, because there is a problem with the last commit (from last September) - Here you can find an open issue about it.
Until this issue would be solved you can still use the script extension with link to previous version as I did:
https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/fdc643759c9aa7e9259da10575e67dc1368e4e9f/prepare_vm_disks.ps1
Add this script extension to the extensionProfile of the virtualMachineProfile of the scale set:
{
"name": "customScript",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.8",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"https://https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/fdc643759c9aa7e9259da10575e67dc1368e4e9f/prepare_vm_disks.ps1"
],
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File prepare_vm_disks.ps1"
}
}
}
As the MS guide template, you need to include a dataDisks section in the storageProfile of the Microsoft.Compute/virtualMachineScaleSets resource(s) and deploy the template.
Here is a working template for your references:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vmSku": {
"type": "string",
"defaultValue": "Standard_A1_v2",
"metadata": {
"description": "Size of VMs in the VM Scale Set."
}
},
"windowsOSVersion": {
"type": "string",
"defaultValue": "2016-Datacenter"
},
"vmssName": {
"type": "string",
"minLength": 3,
"maxLength": 61
},
"instanceCount": {
"type": "int",
"defaultValue": 3,
"minValue": 1,
"maxValue": 100,
"metadata": {
"description": "Number of VM instances (100 or less)."
}
},
"singlePlacementGroup": {
"type": "bool",
"defaultValue": true
},
"adminUsername": {
"type": "string",
"defaultValue": "vmssadmin"
},
"adminPassword": {
"type": "securestring"
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"platformFaultDomainCount": {
"type": "int",
"defaultValue": 1
}
},
"variables": {
"namingInfix": "[toLower(substring(concat(parameters('vmssName'), uniqueString(resourceGroup().id)), 0, 9))]",
"longNamingInfix": "[toLower(parameters('vmssName'))]",
"addressPrefix": "10.0.0.0/16",
"subnetPrefix": "10.0.0.0/24",
"virtualNetworkName": "[concat(variables('namingInfix'), 'vnet')]",
"publicIPAddressName": "[concat(variables('namingInfix'), 'pip')]",
"subnetName": "[concat(variables('namingInfix'), 'subnet')]",
"loadBalancerName": "[concat(variables('namingInfix'), 'lb')]",
"publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
"lbProbeID": "[resourceId('Microsoft.Network/loadBalancers/probes',variables('loadBalancerName'), 'tcpProbe')]",
"natPoolName": "[concat(variables('namingInfix'), 'natpool')]",
"bePoolName": "[concat(variables('namingInfix'), 'bepool')]",
"lbPoolID": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools',variables('loadBalancerName'),variables('bePoolName'))]",
"natStartPort": 50000,
"natEndPort": 50119,
"natBackendPort": 3389,
"nicName": "[concat(variables('namingInfix'), 'nic')]",
"ipConfigName": "[concat(variables('namingInfix'), 'ipconfig')]",
"frontEndIPConfigID": "[resourceId('Microsoft.Network/loadBalancers/frontendIPConfigurations',variables('loadBalancerName'),'loadBalancerFrontEnd')]",
"osType": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "[parameters('windowsOSVersion')]",
"version": "latest"
},
"imageReference": "[variables('osType')]"
},
"resources": [
{
"type": "Microsoft.Network/loadBalancers",
"apiVersion": "2020-06-01",
"name": "[variables('loadBalancerName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
],
"properties": {
"frontendIPConfigurations": [
{
"name": "LoadBalancerFrontEnd",
"properties": {
"publicIPAddress": {
"id": "[variables('publicIPAddressID')]"
}
}
}
],
"backendAddressPools": [
{
"name": "[variables('bePoolName')]"
}
],
"inboundNatPools": [
{
"name": "[variables('natPoolName')]",
"properties": {
"frontendIPConfiguration": {
"id": "[variables('frontEndIPConfigID')]"
},
"protocol": "Tcp",
"frontendPortRangeStart": "[variables('natStartPort')]",
"frontendPortRangeEnd": "[variables('natEndPort')]",
"backendPort": "[variables('natBackendPort')]"
}
}
],
"loadBalancingRules": [
{
"name": "LBRule",
"properties": {
"frontendIPConfiguration": {
"id": "[variables('frontEndIPConfigID')]"
},
"backendAddressPool": {
"id": "[variables('lbPoolID')]"
},
"protocol": "Tcp",
"frontendPort": 80,
"backendPort": 80,
"enableFloatingIP": false,
"idleTimeoutInMinutes": 5,
"probe": {
"id": "[variables('lbProbeID')]"
}
}
}
],
"probes": [
{
"name": "tcpProbe",
"properties": {
"protocol": "Tcp",
"port": 80,
"intervalInSeconds": 5,
"numberOfProbes": 2
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachineScaleSets",
"apiVersion": "2020-06-01",
"name": "[variables('namingInfix')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('vmSku')]",
"tier": "Standard",
"capacity": "[parameters('instanceCount')]"
},
"dependsOn": [
"[resourceId('Microsoft.Network/loadBalancers', variables('loadBalancerName'))]",
"[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]"
],
"properties": {
"overprovision": true,
"upgradePolicy": {
"mode": "Automatic"
},
"singlePlacementGroup": "[parameters('singlePlacementGroup')]",
"platformFaultDomainCount": "[parameters('platformFaultDomainCount')]",
"virtualMachineProfile": {
"storageProfile": {
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage"
},
"dataDisks": [
{
"diskSizeGB": 128,
"lun": 0,
"createOption": "Empty"
}
],
"imageReference": "[variables('imageReference')]"
},
"osProfile": {
"computerNamePrefix": "[variables('namingInfix')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"networkProfile": {
"networkInterfaceConfigurations": [
{
"name": "[variables('nicName')]",
"properties": {
"primary": true,
"ipConfigurations": [
{
"name": "[variables('ipConfigName')]",
"properties": {
"subnet": {
"id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
},
"loadBalancerBackendAddressPools": [
{
"id": "[variables('lbPoolID')]"
}
],
"loadBalancerInboundNatPools": [
{
"id": "[resourceId('Microsoft.Network/loadBalancers/inboundNatPools', variables('loadBalancerName'), variables('natPoolName'))]"
}
]
}
}
]
}
}
]
},
"extensionProfile": {
"extensions": [
{
"name": "customScript",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.8",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.ps1"
],
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File prepare_vm_disks.ps1"
}
}
}
]
}
}
}
},
{
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2020-06-01",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "Static",
"dnsSettings": {
"domainNameLabel": "[variables('longNamingInfix')]"
}
}
},
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-06-01",
"name": "[variables('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
}
]
}
}
],
"outputs": {}
}
If you want to create multiple data disks with the copy function, you can add it like this
"storageProfile": {
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage"
},
"copy": [
{
"name": "dataDisks",
"count": "[parameters('numberOfDataDisks')]",
"input": {
"diskSizeGB": 1023,
"lun": "[copyIndex('dataDisks')]",
"createOption": "Empty"
}
}
],
After my validation, after the above deployment finishs, you need to initialize the disk in each instance, then you could get the expected result.
Reference: https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/tutorial-use-disks-powershell#prepare-the-data-disks
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.
Hi I'm curious if some one can help me with settings for adding environment variables to a vm in an azure template.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminPassword": {
"defaultValue": null,
"type": "SecureString"
},
"processtype": {
"type": "String",
"defaultValue": "sfm",
"allowedValues": [
"sfm",
"mvs",
"mesh",
"text",
"import",
"temp"
]
},
"vmSize": {
"type" : "String",
"defaultValue": "Standard_D12_v2",
"allowedValues": [
"Basic_A2",
"Standard_D12_v2",
"Standard_D13_v2",
"Standard_D14_v2"
]
},
"id": {
"type": "String",
"defaultValue": "0000",
"minLength": 4,
"maxLength": 4
}
},
"variables": {
"location":"northeurope",
"instance_name" : "[toUpper(concat('ps_', parameters('processtype'),'_',parameters('id')))]",
"unique_string": "[uniqueString(resourceGroup().id, variables('instance_name'))]",
"unique_name" : "[toUpper(variables('unique_string'))]",
"networkSecurityGroupName": "[toUpper(concat('nsg',variables('unique_name')))]",
"virtualNetworkName" : "[toUpper(concat('ns', variables('unique_name')))]",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"subnetRef": "[toUpper(concat(variables('vnetID'),'/subnets/',variables('subnetName')))]",
"vnetID":"[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
"publicIPAddressName": "[toUpper(concat('ip', variables('unique_name')))]",
"storageAccountName": "<storageAcName>",
"networkInterfaceName": "[toUpper(concat('ni', variables('unique_name')))]"
},
"resources": [
{
"comments": "Generalized VM",
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat(variables('instance_name'), '_' ,variables('unique_name'))]",
"apiVersion": "2015-06-15",
"location": "[variables('location')]",
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"storageProfile": {
"osDisk": {
"osType": "Windows",
"name": "[variables('unique_name')]",
"createOption": "FromImage",
"image":{
"uri": "<VHDPATH>"
},
"vhd": {
"uri": "[concat('<vhd path>',variables('unique_name'),'.vhd')]"
},
"caching": "ReadWrite"
},
"dataDisks": []
},
"osProfile": {
"computerName": "[parameters('processtype')]",
"adminUsername": "<username>",
"windowsConfiguration": {
"provisionVMAgent": true,
"enableAutomaticUpdates": true
},
"secrets": [],
"adminPassword": "[parameters('adminPassword')]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
]
},
{
"comments": "Generalized Security Group",
"type": "Microsoft.Network/networkSecurityGroups",
"name": "[variables('networkSecurityGroupName')]",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"properties": {
"securityRules": [
{
"name": "default-allow-rdp",
"properties": {
"protocol": "*",
"sourcePortRange": "*",
"destinationPortRange": "3389",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 1000,
"direction": "Inbound"
}
}
]
},
"dependsOn": []
},
{
"comments": "Generalized IP Configuration",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"properties": {
"publicIPAllocationMethod": "Dynamic",
"idleTimeoutInMinutes": 4
},
"dependsOn": []
},
{
"comments": "Generalized virtual network",
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [ "[variables('addressPrefix')]" ]
},
"subnets": [ {
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
} ]
},
"dependsOn": []
},
{
"comments": "Generalized Network Interface",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('networkInterfaceName')]",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[concat(resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName')), '/subnets/', variables('subnetName'))]"
}
}
}
],
"dnsSettings": {
"dnsServers": []
},
"enableIPForwarding": false,
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]",
"[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]",
"[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
]
},
{
"comments": "Generalized storage account.",
"type": "Microsoft.Storage/storageAccounts",
"sku": {
"name": "Standard_LRS",
"tier": "Standard"
},
"kind": "Storage",
"name": "[variables('storageAccountName')]",
"apiVersion": "2016-01-01",
"location": "[variables('location')]",
"tags": {},
"properties": {},
"dependsOn": []
}
]
}
Above is my current template. Where can Ispecify environment variables or some other variable that is accessable inside the vm?