您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用 Azure 门户创建和部署 Azure 资源管理器模板Quickstart: Create and deploy Azure Resource Manager templates by using the Azure portal

了解如何使用 Azure 门户生成资源管理器模板,以及从门户编辑和部署该模板的过程。Learn how to generate a Resource Manager template using the Azure portal, and the process of editing and deploying the template from the portal. Resource Manager 模板为 JSON 文件,用于定义针对解决方案进行部署时所需的资源。Resource Manager templates are JSON files that define the resources you need to deploy for your solution. 若要了解与部署和管理 Azure 解决方案相关联的概念,请参阅 Azure 资源管理器概述To understand the concepts associated with deploying and managing your Azure solutions, see Azure Resource Manager overview.

资源管理器模板快速入门门户图

完成本教程后,部署 Azure 存储帐户。After completing the tutorial, you deploy an Azure Storage account. 可以通过同一过程来部署其他 Azure 资源。The same process can be used to deploy other Azure resources.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

使用门户生成模板Generate a template using the portal

从头开始创建资源管理器模板不是一项容易的任务,尤其是在不熟悉 Azure 部署和 JSON 格式的情况下。Creating a Resource Manager template from scratch is not an easy task, especially if you are new to Azure deployment and you are not familiar with the JSON format. 可以使用 Azure 门户来配置资源(例如 Azure 存储帐户)。Using the Azure portal, you can configure a resource, for example an Azure Storage account. 在部署资源之前,可以将配置导出到资源管理器模板中。Before you deploy the resource, you can export your configuration into a Resource Manager template. 可以保存模板,便于将来重复使用。You can save the template and reuse it in the future.

需要有经验的模板开发人员在尝试部署不熟悉的 Azure 资源时,会使用此方法来生成模板。Many experienced template developers use this method to generate templates when they try to deploy Azure resources that they are not familiar with. 有关使用门户导出模板的更多信息,请参阅将资源组导出到模板For more information about exporting templates by using the portal, see Export resource groups to templates. 查找工作模板的另一种方法是 Azure 快速入门模板The other way to find a working template is from Azure Quickstart templates.

  1. 在 Web 浏览器中转到 Azure 门户,然后登录。In a web browser, go to the Azure portal and sign in.

  2. 在 Azure 门户菜单中,选择“创建资源” 。From the Azure portal menu, select Create a resource.

    在 Azure 门户菜单中,选择“创建资源”

  3. 选择“存储” > “存储帐户” 。Select Storage > Storage account.

    创建 Azure 存储帐户

  4. 输入以下信息:Enter the following information:

    NameName Value
    资源组Resource group 选择“新建” ,然后指定所选资源组的名称。Select Create new, and specify a resource group name of your choice. 在屏幕截图中,资源组名称为 mystorage1016rgOn the screenshot, the resource group name is mystorage1016rg. 资源组是 Azure 资源的容器。Resource group is a container for Azure resources. 资源组方便 Azure 资源的管理。Resource group makes it easier to manage Azure resources.
    名称Name 为存储帐户指定唯一的名称。Give your storage account a unique name. 存储帐户名称在整个 Azure 中必须是唯一的,且仅包含小写字母和数字。The storage account name must be unique across all of Azure, and it contain only lowercase letters and numbers. 名称长度必须介于 3 到 24 个字符之间。Name must be between 3 and 24 characters. 如果收到错误消息“存储帐户名称‘mystorage1016’已被占用”,请尝试使用“<名称>存储<MMDD 格式的今日日期>”,例如“johndolestorage1016” 。If you get an error message saying "The storage account name 'mystorage1016' is already taken", try using <your name>storage<Today's date in MMDD>, for example johndolestorage1016. 有关详细信息,请参阅命名规则和限制For more information, see Naming rules and restrictions.

    可对剩余的属性使用默认值。You can use the default values for the rest of the properties.

    使用 Azure 门户创建 Azure 存储帐户配置

    备注

    某些导出的模板需要经过一些编辑才能部署。Some of the exported templates require some edits before you can deploy them.

  5. 在屏幕底部选择“查看 + 创建”。 Select Review + create on the bottom of the screen. 请勿在下一步中选择“创建” 。Do not select Create in the next step.

  6. 在屏幕底部选择“下载自动化模板”。 Select Download a template for automation on the bottom of the screen. 门户显示生成的模板:The portal shows the generated template:

    通过门户生成模板

    主窗格会显示该模板。The main pane shows the template. 它是包含六个顶级元素(schemacontentVersionparametersvariablesresourcesoutput)的 JSON 文件。It is a JSON file with six top-level elements - schema, contentVersion, parameters, variables, resources, and output. 有关详细信息,请参阅了解 Azure 资源管理器模板的结构和语法For more information, see Understand the structure and syntax of Azure Resource Manager Templates

    定义了六个参数。There are six parameters defined. 其中一个名为 storageAccountNameOne of them is called storageAccountName. 在上一屏幕截图中,第二个突出显示的部分显示如何在模板中引用此参数。The second highlighted part on the previous screenshot shows how to reference this parameter in the template. 在下一部分,请编辑此模板,以便将生成的名称用于存储帐户。In the next section, you edit the template to use a generated name for the storage account.

    在模板中定义了一个 Azure 资源。In the template, one Azure resource is defined. 类型为 Microsoft.Storage/storageAccountsThe type is Microsoft.Storage/storageAccounts. 查看资源定义方式和定义结构。Take a look of how the resource is defined, and the definition structure.

  7. 在屏幕顶部选择“下载” 。Select Download from the top of the screen.

  8. 打开下载的 zip 文件,然后将“template.json”保存到计算机 。Open the downloaded zip file, and then save template.json to your computer. 在下一部分,请使用模板部署工具来编辑此模板。In the next section, you use a template deployment tool to edit the template.

  9. 选择“参数”选项卡,查看为参数提供的值。 Select the Parameter tab to see the values you provided for the parameters. 部署模板时,请写下这些值,下一部分需要它们。Write down these values, you need them in the next section when you deploy the template.

    通过门户生成模板

    使用模板文件和参数文件可以创建一个资源(在本教程中为 Azure 存储帐户)。Using both the template file and the parameters file, you can create a resource, in this tutorial, an Azure storage account.

编辑和部署模板Edit and deploy the template

可以使用 Azure 门户执行一些基本的模板编辑操作。The Azure portal can be used to perform some basic template editing. 在本快速入门中,请使用名为“模板部署”的门户工具。 In this quickstart, you use a portal tool called Template Deployment. 在本教程中使用模板部署,因此可以使用一个界面(Azure 门户)来完成整个教程。Template Deployment is used in this tutorial so you can complete the whole tutorial using one interface - the Azure portal. 若要编辑更复杂的模板,请考虑使用 Visual Studio Code,因为它提供更丰富的编辑功能。To edit a more complex template, consider using Visual Studio Code, which provides richer edit functionalities.

重要

模板部署提供了用于测试简单模板的接口。Template Deployment provides an interface for testing simple templates. 建议不要在生产环境中使用此功能,It is not recommended to use this feature in production. 而是将模板存储在 Azure 存储帐户或 GitHub 等源代码存储库中。Instead, store your templates in an Azure storage account, or a source code repository like GitHub.

Azure 要求每个 Azure 服务都具有唯一的名称。Azure requires that each Azure service has a unique name. 如果输入了已存在的存储帐户名,则部署可能失败。The deployment could fail if you entered a storage account name that already exists. 若要避免此问题,可以修改模板,以便使用模板函数调用 uniquestring(),以生成唯一的存储帐户名。To avoid this issue, you modify the template to use a template function call uniquestring() to generate a unique storage account name.

  1. 从 Azure 门户菜单或“主页”页面,选择“创建资源” 。From the Azure portal menu or from the Home page, select Create a resource.

  2. 在“搜索市场”中键入“模板部署”,然后按 ENTERIn Search the Marketplace, type template deployment, and then press ENTER.

  3. 选择“模板部署”。 Select Template deployment.

    Azure 资源管理器模板库

  4. 选择“创建” 。Select Create.

  5. 选择“在编辑器中生成自己的模板” 。Select Build your own template in the editor.

  6. 选择“加载文件”, 然后按说明加载在上部分下载的 template.json。Select Load file, and then follow the instructions to load template.json you downloaded in the last section.

  7. 对模板进行以下三处更改:Make the following three changes to the template:

    Azure 资源管理器模板

    • 删除“storageAccountName”参数,如上一屏幕截图所示 。Remove the storageAccountName parameter as shown in the previous screenshot.

    • 添加一个名为“storageAccountName”的变量,如上一屏幕截图所示 :Add one variable called storageAccountName as shown in the previous screenshot:

      "storageAccountName": "[concat(uniqueString(subscription().subscriptionId), 'storage')]"
      

      此处使用了两个模板函数:concat()uniqueString()Two template functions are used here: concat() and uniqueString().

    • 更新 Microsoft.Storage/storageAccounts 资源的 name 元素,以使用新定义的变量而不是参数:Update the name element of the Microsoft.Storage/storageAccounts resource to use the newly defined variable instead of the parameter:

      "name": "[variables('storageAccountName')]",
      

      最终的模板应如下所示:The final template shall look like:

      {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "location": {
                "type": "string"
            },
            "accountType": {
                "type": "string"
            },
            "kind": {
                "type": "string"
            },
            "accessTier": {
                "type": "string"
            },
            "supportsHttpsTrafficOnly": {
                "type": "bool"
            }
        },
        "variables": {
            "storageAccountName": "[concat(uniqueString(subscription().subscriptionId), 'storage')]"
        },
        "resources": [
            {
                "name": "[variables('storageAccountName')]",
                "type": "Microsoft.Storage/storageAccounts",
                "apiVersion": "2018-07-01",
                "location": "[parameters('location')]",
                "properties": {
                    "accessTier": "[parameters('accessTier')]",
                    "supportsHttpsTrafficOnly": "[parameters('supportsHttpsTrafficOnly')]"
                },
                "dependsOn": [],
                "sku": {
                    "name": "[parameters('accountType')]"
                },
                "kind": "[parameters('kind')]"
            }
        ],
        "outputs": {}
      }
      
  8. 选择“保存”。 Select Save.

  9. 输入以下值:Enter the following values:

    NameName Value
    资源组Resource group 选择在上一部分中创建的资源组名称。Select the resource group name you created in the last section.
    位置Location 选择存储帐户的位置。Select a location for the storage account. 例如“美国中部”。 For example, Central US.
    帐户类型Account Type 对于本快速入门,请输入 Standard_LRSEnter Standard_LRS for this quickstart.
    种类Kind 对于本快速入门,请输入“StorageV2”。 Enter StorageV2 for this quickstart.
    访问层Access Tier 对于本快速入门,请输入“Hot”。 Enter Hot for this quickstart.
    启用仅限 Https 流量Https Traffic Only Enabled 对于本快速入门,请选择“true”。 Select true for this quickstart.
    我同意上述条款和条件I agree to the terms and conditions stated above (选择)(select)

    下面是示例部署的屏幕截图:Here is a screenshot of a sample deployment:

    Azure 资源管理器模板部署

  10. 选择“购买”。 Select Purchase.

  11. 选择屏幕顶部的铃铛图标(通知)可查看部署状态。Select the bell icon (notifications) from the top of the screen to see the deployment status. 此时会看到“部署正在进行”。 You shall see Deployment in progress. 等待部署完成。Wait until the deployment is completed.

    Azure 资源管理器模板部署通知

  12. 从通知窗格选择“转到资源组” 。Select Go to resource group from the notification pane. 将看到类似于以下的屏幕:You shall see a screen similar to:

    Azure 资源管理器模板部署资源组

    可以看到部署状态为成功,资源组中只有一个存储帐户。You can see the deployment status was successful, and there is only one storage account in the resource group. 存储帐户名称是模板生成的唯一字符串。The storage account name is a unique string generated by the template. 若要详细了解如何使用 Azure 存储帐户,请参阅快速入门:使用 Azure 门户上传、下载和列出 BlobTo learn more about using Azure storage accounts, see Quickstart: Upload, download, and list blobs using the Azure portal.

清理资源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 门户上的左侧菜单中选择“资源组” 。In the Azure portal, select Resource group on the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。 Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name. 应会看到资源组中的存储帐户。You shall see the storage account in the resource group.
  4. 在顶部菜单中选择“删除资源组”。 Select Delete resource group in the top menu.

后续步骤Next steps

本教程已介绍如何通过 Azure 门户生成模板,以及如何使用门户部署模板。In this tutorial, you learned how to generate a template from the Azure portal, and how to deploy the template using the portal. 本快速入门中使用的模板是包含一个 Azure 资源的简单模板。The template used in this Quickstart is a simple template with one Azure resource. 如果模板较为复杂,使用 Visual Studio Code 或 Visual Studio 开发模板会更方便。When the template is complex, it is easier to use Visual Studio Code or Visual Studio to develop the template. 若要详细了解模板开发,请参阅新的初学者教程系列:To learn more about template development, see our new beginner tutorial series: