You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于导出的Azure VM模板创建多VM:如何定义订阅、资源组与区域?

在Azure ARM模板/参数文件中定义订阅、资源组和区域的方法

首先明确:订阅ID和资源组名称无法直接在ARM模板内部定义,这两个属于部署操作的上下文参数,需要在发起部署时指定。而部署区域则可以在模板或参数文件中灵活配置,以下是具体操作方式:

一、订阅与资源组的指定方式

订阅和资源组是部署的目标上下文,必须通过部署命令或Azure门户操作指定:

  • Azure CLI部署示例:
    az deployment group create \
      --subscription <你的订阅ID> \
      --resource-group <目标资源组名称> \
      --template-file ./vm-template.json \
      --parameters @vm-params.json
    
  • PowerShell部署示例:
    New-AzResourceGroupDeployment `
      -SubscriptionId <你的订阅ID> `
      -ResourceGroupName <目标资源组名称> `
      -TemplateFile ./vm-template.json `
      -TemplateParameterFile ./vm-params.json
    

注意:目标资源组需提前创建,若未创建,可在部署时添加创建资源组的逻辑,但通常建议提前完成资源组创建。

二、部署区域的配置方式

方式1:模板内固定区域

直接在模板的资源属性中写死区域值:

{
  "resources": [
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2023-07-01",
      "name": "[parameters('vmName')]",
      "location": "eastus",
      // 其他资源配置属性...
    }
  ]
}

方式2:参数化区域(推荐)

通过模板参数定义区域,支持在参数文件中灵活修改:

  1. 在模板的parameters节点添加区域参数:
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]", // 默认继承资源组的区域
      "metadata": {
        "description": "虚拟机部署的目标区域"
      }
    }
    // 其他参数...
  },
  "resources": [
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2023-07-01",
      "name": "[parameters('vmName')]",
      "location": "[parameters('location')]",
      // 其他资源配置属性...
    }
  ]
}
  1. 在参数文件中指定区域值(可选,用于覆盖默认值):
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "value": "westus2"
    }
  }
}

方式3:自动继承资源组区域

直接在模板中引用资源组的区域,虚拟机将自动部署到与资源组相同的区域:

"location": "[resourceGroup().location]"

内容的提问来源于stack exchange,提问作者Astateofmind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 14:54:19