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

无法从Azure Cloud Shell部署ARM模板,咨询同模板创建资源组是否可行

问题解答

能不能在同一ARM模板中同时创建资源组及其他资源?

可以,但必须通过嵌套部署实现。因为资源组属于订阅级资源,而存储账户这类资源必须部署在资源组范围内,不能直接在同一层级的模板中混合定义不同作用域的资源。

错误原因

你执行的New-AzSubscriptionDeployment是订阅级部署操作,但模板中直接定义了存储账户资源。存储账户需要部署在资源组作用域下,当前部署作用域为订阅,因此触发InvalidScope错误。

解决方案:使用嵌套部署

在订阅级模板中,先定义资源组,再通过嵌套部署将存储账户部署到刚创建的资源组中。

示例主模板(订阅级)

{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "resourceGroupName": {
      "type": "string",
      "defaultValue": "myStorageRG"
    },
    "resourceGroupLocation": {
      "type": "string",
      "defaultValue": "westus3"
    },
    "storageAccountName": {
      "type": "string",
      "defaultValue": "stppuxdatatestwus"
    }
  },
  "resources": [
    // 创建资源组
    {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2021-04-01",
      "name": "[parameters('resourceGroupName')]",
      "location": "[parameters('resourceGroupLocation')]",
      "properties": {}
    },
    // 嵌套部署:将存储账户部署到刚创建的资源组
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "storageAccountDeployment",
      "resourceGroup": "[parameters('resourceGroupName')]",
      "dependsOn": [
        "[resourceId('Microsoft.Resources/resourceGroups', parameters('resourceGroupName'))]"
      ],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2021-06-01",
              "name": "[parameters('storageAccountName')]",
              "location": "[parameters('resourceGroupLocation')]",
              "sku": {
                "name": "Standard_LRS"
              },
              "kind": "StorageV2",
              "properties": {}
            }
          ]
        }
      }
    }
  ]
}

部署命令

你原来的部署命令无需修改,替换为正确的主模板路径即可执行:

New-AzSubscriptionDeployment -Location "westus3" -Name "MyResourceGroupDeployment" -TemplateFile "/home/mary/clouddrive/main-template.json" -TemplateParameterFile "/home/mary/clouddrive/variables.json"

关键要点

  • 订阅级模板的schema为https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#,资源组内资源的模板使用常规schema:https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#
  • 通过dependsOn属性确保资源组创建完成后,再部署存储账户
  • 嵌套部署的resourceGroup属性需指定目标资源组名称

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 18:44:56