教程:使用 ARM 模板创建多个资源实例Tutorial: Create multiple resource instances with ARM templates

了解如何在 Azure 资源管理器 (ARM) 模板中进行迭代操作,以创建 Azure 资源的多个实例。Learn how to iterate in your Azure Resource Manager (ARM) template to create multiple instances of an Azure resource. 在本教程中,你将修改一个模板,以便创建三个存储帐户实例。In this tutorial, you modify a template to create three storage account instances.

“Azure 资源管理器创建多个实例”示意图

本教程涵盖以下任务:This tutorial covers the following tasks:

  • 打开快速入门模板Open a Quickstart template
  • 编辑模板Edit the template
  • 部署模板Deploy the template

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

先决条件Prerequisites

若要完成本文,需要做好以下准备:To complete this article, you need:

打开快速入门模板Open a Quickstart template

Azure 快速入门模板是 ARM 模板的存储库。Azure Quickstart Templates is a repository for ARM templates. 无需从头开始创建模板,只需找到一个示例模板并对其自定义即可。Instead of creating a template from scratch, you can find a sample template and customize it. 本快速入门中使用的模板称为创建标准存储帐户The template used in this quickstart is called Create a standard storage account. 该模板定义 Azure 存储帐户资源。The template defines an Azure Storage account resource.

  1. 在 Visual Studio Code 中,选择“文件”>“打开文件”。 From Visual Studio Code, select File>Open File.

  2. 在“文件名”中粘贴以下 URL:In File name, paste the following URL:

    https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json
    
  3. 选择“打开”以打开该文件。Select Open to open the file.

  4. 在模板中定义了一个“Microsoft.Storage/storageAccounts”资源。There is a 'Microsoft.Storage/storageAccounts' resource defined in the template.

  5. 选择“文件”>“另存为”,将该文件作为 azuredeploy.json 保存到本地计算机。 Select File>Save As to save the file as azuredeploy.json to your local computer.

编辑模板Edit the template

现有模板创建一个存储帐户。The existing template creates one storage account. 请通过自定义模板来创建三个存储帐户。You customize the template to create three storage accounts.

从 Visual Studio Code 中,进行以下四个更改:From Visual Studio Code, make the following four changes:

Azure 资源管理器创建多个实例

  1. 向存储帐户资源定义中添加一个 copy 元素。Add a copy element to the storage account resource definition. 在 copy 元素中,为此循环指定迭代次数和变量。In the copy element, you specify the number of iterations and a variable for this loop. 计数值必须是不超过 800 的正整数。The count value must be a positive integer and can't exceed 800.
  2. copyIndex() 函数返回循环中的当前迭代。The copyIndex() function returns the current iteration in the loop. 使用索引作为名称前缀。You use the index as the name prefix. copyIndex() 从零开始。copyIndex() is zero-based. 若要偏移索引值,可以在 copyIndex() 函数中传递一个值。To offset the index value, you can pass a value in the copyIndex() function. 例如 copyIndex(1)For example, copyIndex(1).
  3. 删除 variables 元素,因为它不再使用。Delete the variables element, because it is not used anymore.
  4. 删除 outputs 元素。Delete the outputs element. 不再需要它。It is no longer needed.

已完成的模板如下所示:The completed template looks like:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[concat(copyIndex(),'storage', uniqueString(resourceGroup().id))]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2",
      "copy": {
        "name": "storagecopy",
        "count": 3
      },
      "properties": {}
    }
  ]
}

有关创建多个实例的详细信息,请参阅在 ARM 模板中部署资源或属性的多个实例For more information about creating multiple instances, see Deploy multiple instances of a resource or property in ARM templates

部署模板Deploy the template

  1. 在本地 Shell 中运行以下命令。From the local Shell, run the following commands. 选择用于显示 PowerShell 代码或 CLI 代码的选项卡。Select the tab to show the PowerShell code or the CLI code.

    echo "Enter a project name that is used to generate resource group name:" &&
    read projectName &&
    echo "Enter the location (i.e. chinaeast):" &&
    read location &&
    resourceGroupName="${projectName}rg" &&
    az group create --name $resourceGroupName --location "$location" &&
    az deployment group create --resource-group $resourceGroupName --template-file "$HOME/azuredeploy.json"
    

若要列出全部三个存储帐户,请省略 --name 参数:To list all three storage accounts, omit the --name parameter:

echo "Enter a project name that is used to generate resource group name:" &&
read projectName &&
resourceGroupName="${projectName}rg" &&
az storage account list --resource-group $resourceGroupName &&
echo "Press [ENTER] to continue ..."

将存储帐户名称与模板中的名称定义进行比较。Compare the storage account names with the name definition in the template.

清理资源Clean up resources

不再需要 Azure 资源时,请通过删除资源组来清理部署的资源。When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group.

  1. 在 Azure 门户上的左侧菜单中选择“资源组”。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name. 应会看到,该资源组中总共有三个资源。You shall see a total of three resources in the resource group.
  4. 在顶部菜单中选择“删除资源组”。Select Delete resource group from the top menu.

后续步骤Next steps

在本教程中,你学习了如何创建多个存储帐户实例。In this tutorial, you learned how to create multiple storage account instances. 在下一教程中,我们将开发包含多个资源和多个资源类型的模板。In the next tutorial, you develop a template with multiple resources and multiple resource types. 某些资源具有依赖的资源。Some of the resources have dependent resources.