Compartir a través de

ARM 模板中的资源声明

若要通过 Azure 资源管理器模板(ARM 模板)部署资源,请添加资源声明。 在 JSON 模板中使用 resources 数组。

languageVersion 2.0 提供了 ARM JSON 模板的增强功能列表,例如将资源声明从数组更改为对象。 本文所示的大多数示例仍使用 resources 数组。 有关 languageVersion 2.0 特定信息,请参阅“使用符号名称”。

提示

我们建议使用 Bicep,因为它提供与 ARM 模板相同的功能,并且该语法更易于使用。 若要了解详细信息,请参阅资源声明

一个模板中最多可以有 800 个资源。 有关详细信息,请参阅模板限制

设置资源类型和版本

将资源添加到模板时,请首先设置资源类型和 API 版本。 这些值决定了可用于资源的其他属性。

下面的示例演示了如何为存储帐户设置资源类型和 API 版本。 示例不显示完整的资源声明。

"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2019-06-01",
    ...
  }
]

设置资源名称

每个资源都有一个名称。 设置资源名称时,请注意资源名称的规则和限制

"parameters": {
  "storageAccountName": {
    "type": "string",
    "minLength": 3,
    "maxLength": 24
  }
},
"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2019-06-01",
    "name": "[parameters('storageAccountName')]",
    ...
  }
]

设置位置

许多资源需要一个位置。 可以通过 Intellisense 来确定资源是否需要位置。 以下示例添加用于存储帐户的位置参数。

"parameters": {
  "storageAccountName": {
    "type": "string",
    "minLength": 3,
    "maxLength": 24
  },
  "location": {
    "type": "string",
    "defaultValue": "[resourceGroup().location]"
  }
},
"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2019-06-01",
    "name": "[parameters('storageAccountName')]",
    "location": "[parameters('location')]",
    ...
  }
]

有关详细信息,请参阅在 ARM 模板中设置资源位置

设置标记

可以在部署期间对资源应用标记。 可以通过标记对部署的资源进行逻辑组织。 有关指定标记的不同方法的示例,请参阅 ARM 模板标记

设置特定于资源的属性

上述属性对于大多数资源类型都是通用的。 设置这些值后,需要设置特定于所部署的资源类型的属性。

使用智能感知或模板引用来确定哪些属性可用以及哪些属性是必需的。 下面的示例将为存储帐户设置其余属性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "functions": [],
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS",
        "tier": "Standard"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot"
      }
    }
  ]
}

使用符号名称

Bicep 中,每个资源定义都具有符号名称。 符号名称用于从 Bicep 文件的其他部分引用资源。 要在 ARM JSON 模板中支持符号名称,请添加版本为 languageVersion 或更高的 2.0,并将资源定义从数组更改为对象。 为模板指定 languageVersion 时,必须为根级别资源指定符号名称。 例如:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.ContainerService/managedClusters",
      ...
    }
  ]
}

可将前面的 JSON 写入以下 JSON:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "resources": {
    "aks": {
      "type": "Microsoft.ContainerService/managedClusters",
      ...
    }
  }
}

符号名称区分大小写。 符号名称的允许字符为字母、数字和 _。 符号名称在模板中必须唯一,但可与模板中的变量名称、参数名称和输出名称重叠。 在以下示例中,存储帐户资源的符号名称与输出的名称相同。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "defaultValue": "[format('storage{0}', uniqueString(resourceGroup().id))]"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": {
    "myStorage": {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    }
  },
  "outputs": {
    "myStorage":{
      "type": "object",
      "value": "[reference('myStorage')]"
    }
  }
}

引用函数可以使用资源的符号名称,如前面的示例所示。 引用函数不能再使用资源的名称,例如,不允许使用 reference(parameters('storageAccountName'))

如果在符号名称部署中使用了部署资源,请使用 apiVersion 2020-09-01 或更高版本。

声明现有资源

使用 languageVersion 2.0 并使用符号名称进行资源声明,可以声明现有资源。 "existing": true 的顶级资源属性会导致 ARM 读取而不是部署资源,如以下示例所示:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "languageVersion": "2.0",

  "resources": {
    "storageAccount": {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "storageacct",
      "existing": true
    }
  },
  "outputs": {
    "saBlocksPlaintext": {
      "type": "bool",
      "value": "[ reference('storageAccount').supportsHttpsTrafficOnly]"
    }
  }
}

现有资源不需要定义除 typeapiVersionname 以外的任何属性。

后续步骤