教程:将资源添加到 ARM 模板Tutorial: Add a resource to your ARM template

前一篇教程中,你已了解如何创建一个空白模板,然后部署该模板。In the previous tutorial, you learned how to create a blank template and deploy it. 现在,可以部署实际的资源。Now, you're ready to deploy an actual resource. 在本教程中,你将添加一个存储帐户。In this tutorial, you add a storage account. 完成本教程大约需要 9 分钟It takes about 9 minutes to complete this tutorial.

先决条件Prerequisites

建议完成有关模板的简介教程,但这不是一项要求。We recommend that you complete the introductory tutorial about templates, but it's not required.

必须已安装带有资源管理器工具扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Resource Manager Tools extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅模板工具For more information, see template tools.

添加资源Add resource

若要将存储帐户定义添加到现有模板,请查看以下示例中突出显示的 JSON。To add a storage account definition to the existing template, look at the highlighted JSON in the following example. 可以复制整个文件并使用其内容替换模板,而无需尝试复制模板的各个节。Instead of trying to copy sections of the template, copy the whole file and replace your template with its contents.

{provide-unique-name} (包括大括号)替换为唯一的存储帐户名称。Replace {provide-unique-name} (including the curly brackets) with a unique storage account name.

重要

存储帐户名称在 Azure 中必须是唯一的。The storage account name must be unique across Azure. 该名称只能包含小写字母或数字。The name must have only lowercase letters or numbers. 其长度不能超过 24 个字符。It can be no longer than 24 characters. 可以尝试使用某种命名模式,例如,使用 store1 作为前缀,然后添加你的姓名首字母缩写和当天的日期。You might try a naming pattern like using store1 as a prefix and then adding your initials and today's date. 例如,使用的名称类似于 store1abc09092019For example, the name you use could look like store1abc09092019 .

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "{provide-unique-name}",
      "location": "chinaeast",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

存储帐户的唯一名称不容易猜出,尤其是在大型部署的自动化中不太适合进行这种猜测。Guessing a unique name for a storage account isn't easy and doesn't work well for automating large deployments. 稍后在本教程系列中,你将使用相应的模板功能来更轻松地创建唯一名称。Later in this tutorial series, you'll use template features that make it easier to create a unique name.

资源属性Resource properties

你可能想要知道如何找到用于每种资源类型的属性。You may be wondering how to find the properties to use for each resource type. 可以使用 ARM 模板参考来查找要部署的资源类型。You can use the ARM template reference to find the resource types you want to deploy.

部署的每个资源至少具有以下三个属性:Every resource you deploy has at least the following three properties:

  • 类型 :资源的类型。type : Type of the resource. 此值是资源提供程序的命名空间和资源类型(例如 Microsoft.Storage/storageAccounts)的组合。This value is a combination of the namespace of the resource provider and the resource type (such as Microsoft.Storage/storageAccounts).
  • apiVersion :用于创建资源的 REST API 版本。apiVersion : Version of the REST API to use for creating the resource. 每个资源提供程序都发布了其自身的 API 版本,因此此值与特定的类型相关。Each resource provider published its own API versions, so this value is specific to the type.
  • 名称 :资源的名称。name : Name of the resource.

大多数资源还有一个 location 属性,该属性用于设置资源部署到的区域。Most resources also have a location property, which sets the region where the resource is deployed.

其他属性因资源类型和 API 版本而异。The other properties vary by resource type and API version. 了解 API 版本与可用属性之间的关联非常重要,因此,让我们直接了解详情。It's important to understand the connection between the API version and the available properties, so let's jump into more detail.

在本教程中,你已将一个存储帐户添加到了模板。In this tutorial, you added a storage account to the template. 可以在 storageAccounts 2019-04-01 中看到该 API 版本。You can see that API version at storageAccounts 2019-04-01. 请注意,你并未将所有属性添加到模板。Notice that you didn't add all of the properties to your template. 许多属性是可选的。Many of the properties are optional. Microsoft.Storage 资源提供程序可能发布了新的 API 版本,但要部署的版本不需要更改。The Microsoft.Storage resource provider could release a new API version, but the version you're deploying doesn't have to change. 可以继续使用该版本,部署的结果是一致的。You can continue using that version and know that the results of your deployment will be consistent.

如果查看较旧的 API 版本(例如 storageAccounts 2016-05-01),你会发现提供了一个较小的属性集。If you view an older API version, such as storageAccounts 2016-05-01, you'll see that a smaller set of properties are available.

如果你决定更改资源的 API 版本,请确保评估该版本的属性,并相应地调整模板。If you decide to change the API version for a resource, make sure you evaluate the properties for that version and adjust your template appropriately.

部署模板Deploy template

可以部署模板来创建存储帐户。You can deploy the template to create the storage account. 为部署指定不同的名称,以便可以在历史记录中轻松找到它。Give your deployment a different name so you can easily find it in the history.

如果尚未创建资源组,请参阅创建资源组If you haven't created the resource group, see Create resource group. 此示例假设已根据 第一篇教程中所述,将 templateFile 变量设置为模板文件的路径。The example assumes you've set the templateFile variable to the path to the template file, as shown in the first tutorial.

New-AzResourceGroupDeployment `
  -Name addstorage `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile

备注

如果部署失败,请使用“详细”开关获取有关正在创建的资源的信息。If the deployment failed, use the verbose switch to get information about the resources being created. 使用“调试”开关获取调试的详细信息。Use the debug switch to get more information for debugging.

你可能会遇到两种可能的部署失败:Two possible deployment failures that you might encounter:

  • 错误:代码=AccountNameInvalid;消息={provide-unique-name} 不是有效的存储帐户名称。Error: Code=AccountNameInvalid; Message={provide-unique-name} is not a valid storage account name. 存储帐户名称长度必须为 3 到 24 个字符,并且只能使用数字和小写字母。Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only.

    在模板中,请将 {provide-unique-name} 替换为唯一的存储帐户名称。In the template, replace {provide-unique-name} with a unique storage account name. 请参阅添加资源See Add resource.

  • 错误:代码=StorageAccountAlreadyTaken;消息=名为 store1abc09092019 的存储帐户已被使用。Error: Code=StorageAccountAlreadyTaken; Message=The storage account named store1abc09092019 is already taken.

    在模板中尝试使用其他存储帐户名称。In the template, try a different storage account name.

由于创建了存储帐户,此部署花费的时间要长于空白模板部署。This deployment takes longer than your blank template deployment because the storage account is created. 这可能需要大约一分钟,但往往不需要这么长时间。It can take about a minute but is usually faster.

验证部署Verify deployment

可以通过在 Azure 门户中浏览资源组来验证部署。You can verify the deployment by exploring the resource group from the Azure portal.

  1. 登录 Azure 门户Sign in to the Azure portal.
  2. 在左侧菜单中选择“资源组”。 From the left menu, select Resource groups .
  3. 选择已部署到的资源组。Select the resource group you deployed to.
  4. 可以看到,已部署了一个存储帐户。You see that a storage account has been deployed.
  5. 请注意,部署标签现在显示:“部署: 2 个已成功”。Notice that the deployment label now says: Deployments: 2 Succeeded .

清理资源Clean up resources

若要继续学习下一篇教程,则不需删除该资源组。If you're moving on to the next tutorial, you don't need to delete the resource group.

如果你不打算继续学习,请删除该资源组以清理部署的资源。If you're stopping now, you might want to 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.
  4. 在顶部菜单中选择“删除资源组”。 Select Delete resource group from the top menu.

后续步骤Next steps

你已创建一个用于部署 Azure 存储帐户的简单模板。You created a simple template to deploy an Azure storage account. 在后面的教程中,你将了解如何向模板添加参数、变量、资源和输出。In the later tutorials, you learn how to add parameters, variables, resources, and outputs to a template. 这些功能是更复杂的模板的构建基块。These features are the building blocks for much more complex templates.