教程:使用资源管理器模板创建多个资源实例Tutorial: Create multiple resource instances with Resource Manager templates

了解如何在 Azure 资源管理器模板中进行迭代操作,以创建 Azure 资源的多个实例。Learn how to iterate in your Azure Resource Manager 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 快速入门模板是资源管理器模板的存储库。Azure QuickStart Templates is a repository for Resource Manager 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/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[concat(copyIndex(),'storage', uniqueString(resourceGroup().id))]",
      "apiVersion": "2018-02-01",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "Storage",
      "properties": {},
      "copy": {
        "name": "storagecopy",
        "count": 3
      }
    }
  ]
}

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

部署模板Deploy the template

有关部署过程,请参阅 Visual Studio Code 快速入门中的部署模板部分。Refer to the Deploy the template section in the Visual Studio Code quickstart for the deployment procedure.

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

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

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az storage account list --resource-group $resourceGroupName

将存储帐户名称与模板中的名称定义进行比较。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 six 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.