为什么会出错;不是有效的资源引用。(代码:InvalidParameter)“;在Azure中尝试通过ARM模板创建VM时触发?

为什么会出错;不是有效的资源引用。(代码:InvalidParameter)“;在Azure中尝试通过ARM模板创建VM时触发?,azure,azure-resource-manager,arm-template,Azure,Azure Resource Manager,Arm Template,我在尝试为VM使用官方MS ARM模板时收到以下错误,无法找出原因: New AzResourceGroupDeployment:7:40:25 AM-部署“VM ARM模板”失败,出现错误。显示1个错误中的1个。 状态消息:Id/subscriptions/…/resourceGroups/testProdData/providers/Microsoft.Network/networkInterfaces/newTestVMNetInt不是有效的 资源参考。(代码:InvalidParamet

我在尝试为VM使用官方MS ARM模板时收到以下错误,无法找出原因:

New AzResourceGroupDeployment:7:40:25 AM-部署“VM ARM模板”失败,出现错误。显示1个错误中的1个。 状态消息:Id/subscriptions/…/resourceGroups/testProdData/providers/Microsoft.Network/networkInterfaces/newTestVMNetInt不是有效的 资源参考。(代码:InvalidParameter)

模板如下所示:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmName": {
      "type": "string",
      "defaultValue": "simpleLinuxVM",
      "metadata": {
        "description": "The name of you Virtual Machine."
      }
    },
    "accounting": {
      "type": "string",
      "defaultValue": "blabla",
      "metadata": {
        "description": "Accounting information"
      }
    },
    "contact": {
      "type": "string",
      "metadata": {
        "description": "use Full name e.g. ..."
      }
    },
    "status": {
      "type": "string",
      "defaultValue": "Test",
      "allowedValues": [
        "Test",
        "Prod"
      ],
      "metadata": {
        "description": "use Test or Prod"
      }
    },
    "adminUsername": {
      "type": "string",
      "defaultValue": "myUserName",
      "metadata": {
        "description": "Username for the Virtual Machine."
      }
    },
    "authenticationType": {
      "type": "string",
      "defaultValue": "password",
      "allowedValues": [
        "sshPublicKey",
        "password"
      ],
      "metadata": {
        "description": "Type of authentication to use on the Virtual Machine. SSH key is recommended."
      }
    },
    "adminPasswordOrKey": {
      "type": "securestring",
      "metadata": {
        "description": "SSH Key or password for the Virtual Machine. SSH key is recommended."
      }
    },
    "dnsLabelPrefix": {
      "type": "string",
      "defaultValue": "[toLower(concat('simplelinuxvm-', uniqueString(resourceGroup().id)))]",
      "metadata": {
        "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
      }
    },
    "ubuntuOSVersion": {
      "type": "string",
      "defaultValue": "18.04-LTS",
      "allowedValues": [],
      "metadata": {
        "description": "The Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "VmSize": {
      "type": "string",
      "defaultValue": "Standard_D2s_v3",
      "metadata": {
        "description": "The size of the VM"
      }
    },
    "virtualNetworkName": {
      "type": "string",
      "defaultValue": "vNet",
      "metadata": {
        "description": "Name of the VNET"
      }
    },
    "subnetName": {
      "type": "string",
      "defaultValue": "Subnet",
      "metadata": {
        "description": "Name of the subnet in the virtual network"
      }
    },
    "networkSecurityGroupName": {
      "type": "string",
      "defaultValue": "SecGroupNet",
      "metadata": {
        "description": "Name of the Network Security Group"
      }
    }
  },
  "variables": {
    "publicIpAddressName": "[concat(parameters('vmName'), 'PublicIP' )]",
    "networkInterfaceName": "[concat(parameters('vmName'),'NetInt')]",
    "subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnetName'))]",
    "osDiskType": "Standard_LRS",
    "subnetAddressPrefix": "10.1.0.0/24",
    "addressPrefix": "10.1.0.0/16",
    "linuxConfiguration": {
      "disablePasswordAuthentication": false
      // "ssh": {
      //   "publicKeys": [
      //     {
      //       "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
      //       "keyData": "[parameters('adminPasswordOrKey')]"
      //     }
      //   ]
      // }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Network/networkInterfaces",
      "apiVersion": "2020-06-01",
      "name": "[variables('networkInterfaceName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
        "[resourceId('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
        "[resourceId('Microsoft.Network/publicIpAddresses/', variables('publicIpAddressName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "privateIPAllocationMethod": "Dynamic",
              "publicIpAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
              }
            }
          }
        ],
        "networkSecurityGroup": {
          "id": "[resourceId('Microsoft.Network/networkSecurityGroups',parameters('networkSecurityGroupName'))]"
        }
      }
    },
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2020-06-01",
      "name": "[parameters('networkSecurityGroupName')]",
      "location": "[parameters('location')]",
      "properties": {
        "securityRules": [
          {
            "name": "SSH",
            "properties": {
              "protocol": "TCP",
              "sourcePortRange": "*",
              "destinationPortRange": "22",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 300,
              "direction": "Inbound",
              "sourcePortRanges": [],
              "destinationPortRanges": [],
              "sourceAddressPrefixes": [],
              "destinationAddressPrefixes": []
            }
          },
          {
            "name": "Port_1883",
            "properties": {
              "protocol": "*",
              "sourcePortRange": "*",
              "destinationPortRange": "1883",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 310,
              "direction": "Inbound",
              "sourcePortRanges": [],
              "destinationPortRanges": [],
              "sourceAddressPrefixes": [],
              "destinationAddressPrefixes": []
            }
          },
          {
            "name": "Port_80",
            "properties": {
              "protocol": "*",
              "sourcePortRange": "*",
              "destinationPortRange": "80",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 320,
              "direction": "Inbound",
              "sourcePortRanges": [],
              "destinationPortRanges": [],
              "sourceAddressPrefixes": [],
              "destinationAddressPrefixes": []
            }
          },
          {
            "name": "Port_8080",
            "properties": {
              "protocol": "*",
              "sourcePortRange": "*",
              "destinationPortRange": "443",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 330,
              "direction": "Inbound",
              "sourcePortRanges": [],
              "destinationPortRanges": [],
              "sourceAddressPrefixes": [],
              "destinationAddressPrefixes": []
            }
          },
          {
            "name": "Port_8883",
            "properties": {
              "protocol": "*",
              "sourcePortRange": "*",
              "destinationPortRange": "8883",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 340,
              "direction": "Inbound",
              "sourcePortRanges": [],
              "destinationPortRanges": [],
              "sourceAddressPrefixes": [],
              "destinationAddressPrefixes": []
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2020-06-01",
      "name": "[parameters('virtualNetworkName')]",
      "location": "[parameters('location')]",
      "tags": {
        "status": "[parameters('status')]",
        "accounting": "[parameters('accounting')]",
        "extint": "intern",
        "contact": "[parameters('contact')]"
      },
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('addressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[parameters('subnetName')]",
            "properties": {
              "addressPrefix": "[variables('subnetAddressPrefix')]",
              "privateEndpointNetworkPolicies": "Enabled",
              "privateLinkServiceNetworkPolicies": "Enabled"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/publicIpAddresses",
      "apiVersion": "2020-06-01",
      "name": "[variables('publicIpAddressName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Basic",
        "tier": "Regional"
      },
      "properties": {
        "publicIpAllocationMethod": "Dynamic",
        "publicIPAddressVersion": "IPv4",
        "dnsSettings": {
          "domainNameLabel": "[parameters('dnsLabelPrefix')]"
        },
        "idleTimeoutInMinutes": 4
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2020-06-01",
      "name": "[parameters('vmName')]",
      "location": "[parameters('location')]",
      "tags": {
        "status": "[parameters('status')]",
        "accounting": "[parameters('accounting')]",
        "extint": "intern",
        "contact": "[parameters('contact')]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('VmSize')]"
        },
        "storageProfile": {
          "osDisk": {
            "createOption": "fromImage",
            "caching": "ReadWrite",
            "managedDisk": {
              "storageAccountType": "[variables('osDiskType')]",
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
            },
            "diskSizeGB": 30
          },
          "imageReference": {
            "publisher": "Canonical",
            "offer": "UbuntuServer",
            "sku": "[parameters('ubuntuOSVersion')]",
            "version": "latest"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": true
          }
        },
        "osProfile": {
          "computerName": "[parameters('vmName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPasswordOrKey')]",
          "linuxConfiguration": "[if(equals(parameters('authenticationType'), 'password'), json('null'), variables('linuxConfiguration'))]"
        }
      }
    }
  ],
  "outputs": {
    "adminUsername": {
      "type": "string",
      "value": "[parameters('adminUsername')]"
    },
    "hostname": {
      "type": "string",
      "value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
    },
    "sshCommand": {
      "type": "string",
      "value": "[concat('ssh ', parameters('adminUsername'), '@', reference(variables('publicIPAddressName')).dnsSettings.fqdn)]"
    }
  }
}
powershell脚本如下所示:

New-AzResourceGroup -Name $rg2 -Location westeurope -Force
New-AzResourceGroupDeployment `
    -vmName 'newTestVM' `
    -ResourceGroupName $rg2 `
    -TemplateFile '.\VM-ARM-Template.json' `
    -contact 'blabla' `
    -status 'Test' `
    -adminUsername 'myUser' `
    -authenticationType 'password' `
    -adminPasswordOrKey ("adna234V0sad=sadklandv" | ConvertTo-SecureString -AsPlainText -Force)

运行脚本后,Azure门户中至少会显示出令人惊讶的NetworkInterface,通过查看json,我获得了与错误描述中给出的相同的ID。我无法解释为什么会抛出is错误。有什么想法吗?

您正在将受管磁盘设置为NIC。您需要修复此ID以指向受管磁盘

        "managedDisk": {
          "storageAccountType": "[variables('osDiskType')]",
          "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
        },

对为我工作!谢谢我想知道这一行是怎么加上去的。可能是电脑前的那个人。你是如何缩小错误范围的?通过错误消息,我不清楚这一行是否导致了错误。为了找到这一点,我选择了ARM抛出错误的资源类型(networkInterfaces),并在模板中搜索正在使用的所有地方,以查看错误的内容(即格式不正确、模板函数语法错误、位置不正确等)。您可能还想查看二头肌()。它是一种用于构建ARM部署的声明式脚本语言。目标之一是使语法比JSON更简洁、更直观。