你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ARM 模板中的资源声明

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

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 模板标记

设置特定于资源的属性

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

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

{
  "$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 符号名称进行资源声明时,可以声明现有资源。 导致 ARM 读取而不是部署资源的顶级资源属性 "existing": true ,如以下示例所示:

{
  "$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之外的任何属性。

后续步骤