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

教程:创建和部署第一个 Azure 资源管理器模板Tutorial: Create and deploy your first Azure Resource Manager template

本教程介绍 Azure 资源管理器模板。This tutorial introduces you to Azure Resource Manager templates. 其中将会说明如何创建初学者模板并将其部署到 Azure。It shows you how to create a starter template and deploy it to Azure. 本教程还将介绍模板的结构,以及使用模板时所需的工具。You'll learn about the structure of the template and the tools you'll need for working with templates. 完成本教程大约需要 12 分钟,但实际时间根据需要安装的工具数量而异。It takes about 12 minutes to complete this tutorial, but the actual time will vary based on how many tools you need to install.

本文是相关教学系列的第一篇教程。This tutorial is the first of a series. 在学习该教学系列的过程中,你将逐步修改入门模板,直到学会了资源管理器模板的所有核心组成部分。As you progress through the series, you modify the starting template step-by-step until you've explored all of the core parts of a Resource Manager template. 这些元素是更复杂的模板的构建基块。These elements are the building blocks for much more complex templates. 我们希望在本教学系列结束时,你能够自信地创建自己的模板,并准备好使用这些模板自动完成部署。We hope by the end of the series you're confident creating your own templates and ready to automate your deployments with templates.

若要了解使用模板的好处,以及为何要使用模板自动完成部署,请参阅 Azure 资源管理器模板If you want to learn about the benefits of using templates and why you should automate deployment with templates, see Azure Resource Manager templates.

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

获取工具Get tools

首先,请确保已获取创建和部署模板所需的工具。Let's start by making sure you have the tools you need to create and deploy templates.

编辑器Editor

模板是一些 JSON 文件。Templates are JSON files. 若要创建模板,需要一个好用的 JSON 编辑器。To create templates, you need a good JSON editor. 我们建议使用带有资源管理器工具扩展的 Visual Studio Code。We recommend Visual Studio Code with the Resource Manager Tools extension. 如果需要安装这些工具,请参阅使用 Visual Studio Code 创建 Azure 资源管理器模板If you need to install these tools, see Use Visual Studio Code to create Azure Resource Manager templates.

命令行部署Command-line deployment

还需要使用 Azure PowerShell 或 Azure CLI 来部署模板。You also need either Azure PowerShell or Azure CLI to deploy the template. 有关安装说明,请参阅:For the installation instructions, see:

安装 Azure PowerShell 或 Azure CLI 后,请务必完成首次登录。After installing either Azure PowerShell or Azure CLI, make sure you sign in for the first time. 有关帮助,请参阅登录 - PowerShell登录 - Azure CLIFor help, see Sign in - PowerShell or Sign in - Azure CLI.

好了,现在可以开始了解模板。Okay, you're ready to start learning about templates.

创建第一个模板Create your first template

  1. 打开装有资源管理器工具扩展的 Visual Studio Code。Open Visual Studio Code with the Resource Manager Tools extension installed.

  2. 在“文件”菜单中,选择“新建文件”以创建新的文件。 From the File menu, select New File to create a new file.

  3. 在“文件”菜单中选择“另存为”。 From the File menu, select Save as.

  4. 将该文件命名为 azuredeploy,然后选择“JSON”文件扩展名。 Name the file azuredeploy and select the JSON file extension. 完整的文件名为 azuredeploy.jsonThe complete name of the file azuredeploy.json.

  5. 将该文件保存到工作站。Save the file to your workstation. 选择容易记住的路径,因为稍后在部署模板时需要提供该路径。Select a path that is easy to remember because you'll provide that path later when deploying the template.

  6. 将以下 JSON 内容复制并粘贴到该文件中:Copy and paste the following JSON into the file:

    {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "resources": []
    }
    

    VS Code 环境如下所示:Here's what your VS Code environment looks like:

    资源管理器模板 - Visual Studio Code First 模板

    此模板不部署任何资源。This template doesn't deploy any resources. 我们从一个空白模板开始,以便可以熟悉模板的部署步骤,同时尽量减少出错的可能性。We're starting with a blank template so you can get familiar with the steps to deploy a template while minimizing the chance of something going wrong.

    JSON 文件包含以下元素:The JSON file has these elements:

    • $schema:指定 JSON 架构文件的位置。$schema: Specifies the location of the JSON schema file. 架构文件描述模板中可用的属性。The schema file describes the properties that are available within a template. 例如,架构定义 resources 作为模板的有效属性之一。For example, the schema defines resources as one of the valid properties for a template. 架构的日期为 2015-01-01,请不要担心。Don't worry that the date for the schema is 2015-01-01. 此架构版本是最新的,其中包含所有最新功能。This schema version is up-to-date and includes all of the latest features. 之所以未更改架构日期,是因为自从推出以来,它尚未发生重大更改。The schema date hasn't been changed because there have been no breaking changes since its introduction.
    • contentVersion:指定模板的版本(例如 1.0.0.0)。contentVersion: Specifies the version of the template (such as 1.0.0.0). 可为此元素提供任意值。You can provide any value for this element. 使用此值记录模板中的重要更改。Use this value to document significant changes in your template. 使用模板部署资源时,此值可用于确保使用正确的模板。When deploying resources using the template, this value can be used to make sure that the right template is being used.
    • resources:包含要部署或更新的资源。resources: Contains the resources you want to deploy or update. 此元素目前是空的,但稍后要添加资源。Currently, it's empty, but you'll add resources later.
  7. 保存文件。Save the file.

祝贺你,现已创建第一个模板。Congratulations, you've created your first template.

登录 AzureSign in to Azure

若要开始使用 Azure PowerShell/Azure CLI,请使用你的 Azure 凭据登录。To start working with Azure PowerShell/Azure CLI, sign in with your Azure credentials.

Connect-AzAccount

创建资源组Create resource group

部署模板时,请指定一个包含资源的资源组。When you deploy a template, you specify a resource group that will contain the resources. 在运行部署命令之前,请使用 Azure CLI 或 Azure PowerShell 创建该资源组。Before running the deployment command, create the resource group with either Azure CLI or Azure PowerShell. 使用以下代码部分中的选项卡在 Azure PowerShell 与 Azure CLI 之间进行选择。Select the tabs in the following code section to choose between Azure PowerShell and Azure CLI. 本文中的 CLI 示例针对 Bash shell 编写。The CLI examples in this article are written for the Bash shell.

New-AzResourceGroup `
  -Name myResourceGroup `
  -Location "Central US"

部署模板Deploy template

若要部署模板,请使用 Azure CLI 或 Azure PowerShell。To deploy the template, use either Azure CLI or Azure PowerShell. 使用创建的资源组。Use the resource group you created. 为部署指定一个适当的名称,以便可以在部署历史记录中轻松识别该部署。Give a name to the deployment so you can easily identify it in the deployment history. 为方便起见,另请创建一个用于存储模板文件路径的变量。For convenience, also create a variable that stores the path to the template file. 使用此变量可以更轻松地运行部署命令,因为无需在每次部署时重新键入路径。This variable makes it easier for you to run the deployment commands because you don't have to retype the path every time you deploy.

$templateFile = "{provide-the-path-to-the-template-file}"
New-AzResourceGroupDeployment `
  -Name blanktemplate `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile

部署命令将返回结果。The deployment command returns results. 查看 ProvisioningState 以确定部署是否成功。Look for ProvisioningState to see whether the deployment succeeded.

验证部署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 deploy in the last procedure. 默认名称为 myResourceGroupThe default name is myResourceGroup. 你应会看到,该资源组中未部署任何资源。You shall see no resource deployed within the resource group.

  4. 请注意,在概述的右上角显示了部署状态。Notice in the upper right of the overview, the status of the deployment is displayed. 选择“1 成功”。 Select 1 Succeeded.

    查看部署状态

  5. 将会看到该资源组的部署历史记录。You see a history of deployment for the resource group. 选择“blanktemplate”。 Select blanktemplate.

    选择部署

  6. 将会看到部署摘要。You see a summary of the deployment. 在本例中,显示的信息不是很多,因此尚未部署任何资源。In this case, there's not a lot to see because no resources were deployed. 在本教学系列的后续教程中,你可能会发现,查看部署历史记录中的摘要非常有用。Later in this series you might find it helpful to review the summary in the deployment history. 请注意,在左侧可以查看部署过程中使用的输入、输出和模板。Notice on the left you can view inputs, outputs, and the template used during deployment.

    查看部署摘要

清理资源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 delete 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 to Azure. 在下一篇教程中,你要将一个存储帐户添加到该模板,然后将其部署到资源组。In the next tutorial, you'll add a storage account to the template and deploy it to your resource group.