Azure ARM模板:创建资源组

18

我们对ARM(Azure资源管理器)模板还不熟悉。在制作一个模板时,我注意到在部署模板时需要提供资源组。

是否可以像其他资源一样通过模板创建资源组呢?

5个回答

14

现在,您可以使用 ARM 模板创建资源组。您可以使用以下模板。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "rgLocation": {
      "type": "string",
      "defaultValue": "Southeast Asia"
    },
    "rgName": {
      "type": "string",
      "defaultValue": "myResourceGroup"
    }
  },
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2018-05-01",
      "location": "[parameters('rgLocation')]",
      "name": "[parameters('rgName')]"
    }
  ],
  "outputs": {}
}

您可以使用Azure CLI运行此操作。但是您必须安装最新的CLI版本。我安装了版本2.0.43。这包括使用az deployment命令进行订阅级别部署。

要执行此操作,请运行以下命令。

az deployment create --name <deployment_name> --location <resource_location> --template-file .\azuredeploy.json


这对我不起作用。我得到了以下(UUID已编辑){"code":"DeploymentFailed","message":"至少有一个资源部署操作失败。请列出详细的部署操作。请参阅https://aka.ms/DeployOperations以获取使用详细信息。","details":[{"code":"NotFound","message":"{\r\n \"message\": \"找不到与请求URI 'https://management.azure.com/subscriptions/<UUID>/resourcegroups/MyResourceGroup/providers/Microsoft.Resources/resourceGroups/myResourceGroup?api-version=2018-05-01'匹配的HTTP资源。\"\r\n}"}]} - lorenzo
这个解决方案是错误的,不起作用,它正在使用错误的架构,下面的解决方案是正确的。 - Jan
1
az CLI 给出了一个警告,指出 deployment create 已被弃用,并将在未来的版本中删除。应改用 deployment sub create - tuomastik
请注意,从今天开始,存在着不同的部署范围,由$schema指示。 - undefined

6

接受的解决方案是错误的。资源组是在订阅级别上部署的,而不是在资源组级别上部署的。难怪它不起作用。

请注意 $schema 的差异。应该使用 subscriptionDeploymentTemplate

{
    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "functions": [],
    "variables": {},
    "resources": [
        {
            "name": "string",
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2020-10-01",
            "location": "string",
            "tags": {},
            "properties": {
                                
            }
        }
    ],
    "outputs": {}
}

这对我有用!如果您正在使用Azure CLI,请记得使用az deployment sub create运行它,这样您就可以在订阅内创建新的资源组。 - Wojciech X

5

该内容现已发布在Microsoft文档中,

az deployment create \
  -n demoEmptyRG \
  -l southcentralus \
  --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/emptyRG.json \
  --parameters rgName=demoRG rgLocation=northcentralus

0
现在,我们无法使用 ARM 模板来创建 Azure 资源组。是否可以像其他资源一样通过模板创建资源组?

1
为什么?有什么特别的原因吗? - BlindSniper
默认情况下,我们将使用Azure PowerShell或Azure CLI使用JSON文件创建新的部署。以下是PowerShell命令示例:New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName ExampleResourceGroup -TemplateFile c:\Users\Desktop\jasontest2.json 在此脚本中,我们已经指定了资源组。 - Jason Ye
@BlindSniper,基本上是一个鸡生蛋蛋生鸡的情况。资源组是在订阅级别创建的。因此,无法使用deploymentParameters.json模式。而是使用subscriptionDeploymentTemplate.json模式。 - undefined

-2

必须使用subscriptionDeploymentTemplate.json模式。


1
尝试包含相关的代码块或步骤来帮助原始发布者。 - Jay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接