教程:创建包含所依赖资源的 ARM 模板Tutorial: Create ARM templates with dependent resources

了解如何创建 Azure 资源管理器 (ARM) 模板以部署多个资源并配置部署顺序。Learn how to create an Azure Resource Manager (ARM) template to deploy multiple resources and configure the deployment order. 创建模板后,使用本地 Shell 部署该模板。After you create the template, you deploy the template using the local Shell.

本教程介绍如何创建存储帐户、虚拟机、虚拟网络以及一些其他的依赖资源。In this tutorial, you create a storage account, a virtual machine, a virtual network, and some other dependent resources. 某些资源的部署依赖于另一资源的存在。Some of the resources cannot be deployed until another resource exists. 例如,创建虚拟机的前提是其存储帐户和网络接口存在。For example, you can't create the virtual machine until its storage account and network interface exist. 可通过将一个资源标记为依赖于其他资源来定义此关系。You define this relationship by making one resource as dependent on the other resources. Resource Manager 将评估资源之间的依赖关系,并根据其依赖顺序进行部署。Resource Manager evaluates the dependencies between resources, and deploys them in their dependent order. 如果资源互不依赖,资源管理器将以并行方式部署资源。When resources aren't dependent on each other, Resource Manager deploys them in parallel. 有关详细信息,请参阅在 ARM 模板中定义部署资源的顺序For more information, see Define the order for deploying resources in ARM templates.

资源管理器模板依赖资源部署顺序图

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

  • 打开快速入门模板Open a Quickstart template
  • 浏览模板Explore the template
  • 部署模板Deploy the template

如果没有 Azure 订阅,请在开始前创建一个试用订阅If you don't have an Azure subscription, create a Trial Subscription 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. 本教程中使用的模板称为部署简单的 Windows VMThe template used in this tutorial is called Deploy a simple Windows VM.

  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-vm-simple-windows/azuredeploy.json
    
  3. 选择“打开”以打开该文件。Select Open to open the file.

  4. 选择“文件”>“另存为”,将该文件的副本保存到名为 azuredeploy.json 的本地计算机。 Select File>Save As to save a copy of the file to your local computer with the name azuredeploy.json.

浏览模板Explore the template

浏览此部分的模板时,请尝试回答以下问题:When you explore the template in this section, try to answer these questions:

  • 在此模板中定义了多少 Azure 资源?How many Azure resources defined in this template?
  • 其中一个资源是 Azure 存储帐户。One of the resources is an Azure storage account. 该定义是否与上一教程中使用的定义类似?Does the definition look like the one used in the last tutorial?
  • 对于此模板中定义的资源,能否找到模板参考?Can you find the template references for the resources defined in this template?
  • 能否找到资源的依赖项?Can you find the dependencies of the resources?
  1. 在 Visual Studio Code 中折叠元素,直到只能在 resources 中看到第一级元素和第二级元素:From Visual Studio Code, collapse the elements until you only see the first-level elements and the second-level elements inside resources:

    Visual Studio Code Azure 资源管理器模板

    有六个通过此模板定义的资源:There are six resources defined by the template:

    • Microsoft.Storage/storageAccountsMicrosoft.Storage/storageAccounts.
    • Microsoft.Network/publicIPAddressesMicrosoft.Network/publicIPAddresses.
    • Microsoft.Network/networkSecurityGroupsMicrosoft.Network/networkSecurityGroups.
    • Microsoft.Network/virtualNetworksMicrosoft.Network/virtualNetworks.
    • Microsoft.Network/networkInterfacesMicrosoft.Network/networkInterfaces.
    • Microsoft.Compute/virtualMachinesMicrosoft.Compute/virtualMachines.

    在自定义模板之前查看模板参考会很有帮助。It is helpful to review the template reference before customizing a template.

  2. 展开第一个资源。Expand the first resource. 它是一个存储帐户。It is a storage account.

    Visual Studio Code Azure 资源管理器模板存储帐户定义

  3. 展开第二个资源。Expand the second resource. 资源类型为 Microsoft.Network/publicIPAddressesThe resource type is Microsoft.Network/publicIPAddresses.

    Visual Studio Code Azure 资源管理器模板公共 IP 地址定义

  4. 展开第三个资源。Expand the third resource. 资源类型为 Microsoft.Network/networkSecurityGroupsThe resource type is Microsoft.Network/networkSecurityGroups.

    Visual Studio Code Azure 资源管理器模板网络安全组定义

  5. 展开第四个资源。Expand the fourth resource. 资源类型为 Microsoft.Network/virtualNetworksThe resource type is Microsoft.Network/virtualNetworks:

    Visual Studio Code Azure 资源管理器模板虚拟网络 dependsOn

    使用 dependsOn 元素可将一个资源定义为与一个或多个资源相依赖。The dependsOn element enables you to define one resource as a dependent on one or more resources. 此资源依赖于另一个资源:This resource depends on one other resource:

    • Microsoft.Network/networkSecurityGroups
  6. 展开第五个资源。Expand the fifth resource. 资源类型为 Microsoft.Network/networkInterfacesThe resource type is Microsoft.Network/networkInterfaces. 此资源依赖于两个其他的资源:The resource depends on two other resources:

    • Microsoft.Network/publicIPAddresses
    • Microsoft.Network/virtualNetworks
  7. 展开第六个资源。Expand the sixth resource. 此资源为虚拟机。This resource is a virtual machine. 它依赖于两个其他的资源:It depends on two other resources:

    • Microsoft.Storage/storageAccounts
    • Microsoft.Network/networkInterfaces

下图演示了此模板的资源和依赖项信息:The following diagram illustrates the resources and the dependency information for this template:

Visual Studio Code Azure 资源管理器模板依赖项图

指定依赖项可以让资源管理器有效地部署此解决方案。By specifying the dependencies, Resource Manager efficiently deploys the solution. 它以并行方式部署存储帐户、公共 IP 地址和虚拟网络,因为这些没有依赖项。It deploys the storage account, public IP address, and virtual network in parallel because they have no dependencies. 部署公共 IP 地址和虚拟网络资源以后,会创建网络接口。After the public IP address and virtual network are deployed, the network interface is created. 所有其他的资源都部署以后,资源管理器会部署虚拟机。When all other resources are deployed, Resource Manager deploys the virtual machine.

部署模板Deploy the template

  1. 选择前面在本教程中保存的模板。Select the template you saved earlier in the tutorial. 默认名称为 azuredeploy.jsonThe default name is azuredeploy.json. 如果某个文件的文件名相同,则会覆盖旧文件,没有任何通知。If you have a file with the same file name, the old file is overwritten without any notification.

  2. 运行以下 PowerShell 脚本以部署该模板。Run the following PowerShell script to deploy the template.

    echo "Enter a project name that is used to generate resource group name:" &&
    read projectName &&
    echo "Enter the location (i.e. chinaeast):" &&
    read location &&
    echo "Enter the virtual machine admin username:" &&
    read adminUsername &&
    echo "Enter the DNS label prefix:" &&
    read dnsLabelPrefix &&
    resourceGroupName="${projectName}rg" &&
    az group create --name $resourceGroupName --location $location &&
    az deployment group create --resource-group $resourceGroupName --template-file "azuredeploy.json" --parameters adminUsername=$adminUsername dnsLabelPrefix=$dnsLabelPrefix
    
  3. 通过 RDP 连接到虚拟机,验证虚拟机是否已成功创建。RDP to the virtual machine to verify the virtual machine has been created successfully.

清理资源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'll see a total of six resources in the resource group.
  4. 在顶部菜单中选择“删除资源组”。Select Delete resource group from the top menu.