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

快速入门:使用 Visual Studio Code 创建 Azure 资源管理器模板Quickstart: Create Azure Resource Manager templates by using Visual Studio Code

了解如何使用 Visual Studio Code 和 Azure 资源管理器工具扩展创建和编辑 Azure 资源管理器模板。Learn how to use Visual Studio code and the Azure Resource Manager Tools extension to create and edit Azure Resource Manager templates. 可以在 Visual Studio Code 中不使用扩展创建资源管理器模板,但是该扩展提供自动完成选项,可以简化模板开发。You can create Resource Manager templates in Visual Studio Code without the extension, but the extension provides autocomplete options that simplify template development. 若要了解与部署和管理 Azure 解决方案相关联的概念,请参阅 Azure 资源管理器概述To understand the concepts associated with deploying and managing your Azure solutions, see Azure Resource Manager overview.

在本教程中,你将部署存储帐户:In this tutorial, you deploy a storage account:

资源管理器模板快速入门 visual studio 代码关系图

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

先决条件Prerequisites

若要完成本文,需要做好以下准备:To complete this article, you need:

  • Visual Studio CodeVisual Studio Code.

  • 资源管理器工具扩展。Resource Manager Tools extension. 若要安装此扩展,请使用以下步骤:To install, use these steps:

    1. 打开 Visual Studio Code。Open Visual Studio Code.
    2. CTRL+SHIFT+X 打开“扩展”窗格Press CTRL+SHIFT+X to open the Extensions pane
    3. 搜索“Azure 资源管理器工具”,然后选择“安装”。Search for Azure Resource Manager Tools, and then select Install.
    4. 选择“重新加载”完成扩展安装。Select Reload to finish the extension installation.

打开快速入门模板Open a Quickstart template

无需从头开始创建模板,可以通过 Azure 快速入门模板打开一个模板。Instead of creating a template from scratch, you open a template from Azure Quickstart Templates. Azure 快速入门模板是资源管理器模板的存储库。Azure QuickStart Templates is a repository for Resource Manager templates.

本快速入门中使用的模板称为创建标准存储帐户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. 选择“文件”>“另存为”,将该文件作为 azuredeploy.json 保存到本地计算机。Select File>Save As to save the file as azuredeploy.json to your local computer.

编辑模板Edit the template

若要体验如何使用 Visual Studio Code 来编辑模板,请将额外的一个元素添加到 outputs 节以显示存储 URI。To experience how to edit a template using Visual Studio Code, you add one more element into the outputs section to show the storage URI.

  1. 将额外的一个输出添加到导出的模板:Add one more output to the exported template:

    "storageUri": {
      "type": "string",
      "value": "[reference(variables('storageAccountName')).primaryEndpoints.blob]"
    }
    

    完成后,outputs 节如下所示:When you are done, the outputs section looks like:

    "outputs": {
      "storageAccountName": {
        "type": "string",
        "value": "[variables('storageAccountName')]"
      },
      "storageUri": {
        "type": "string",
        "value": "[reference(variables('storageAccountName')).primaryEndpoints.blob]"
      }
    }
    

    如果在 Visual Studio Code 中复制并粘贴了代码,请尝试重新键入 value 元素,以体验资源管理器工具扩展的 IntelliSense 功能。If you copied and pasted the code inside Visual Studio Code, try to retype the value element to experience the IntelliSense capability of the Resource Manager Tools extension.

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

  2. 选择“文件”>“保存”以保存文件。Select File>Save to save the file.

部署模板Deploy the template

可通过多种方法来部署模板。There are many methods for deploying templates. 在本快速入门中将使用 Azure Cloud shell。Azure Cloud shell is used in this quickstart. cloud shell 支持 Azure CLI 和 Azure PowerShell。The cloud shell supports both Azure CLI and Azure PowerShell. 使用选项卡选择器在 CLI 和 PowerShell 之间进行选择。Use the tab selector to choose between CLI and PowerShell.

备注

本文进行了更新,以便使用新的 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.

  1. 登录到 Azure Cloud ShellSign in to the Azure Cloud shell

  2. 通过在左上角选择“PowerShell”或“Bash”(CLI) 来选择你喜欢使用的环境。Choose your preferred environment by selecting either PowerShell or Bash(CLI) on the upper left corner. 进行切换时,需重启 shell。Restarting the shell is required when you switch.

  3. 依次选择“上传/下载文件”、“上传”。Select Upload/download files, and then select Upload.

    选择在上一部分保存的文件。Select the file you saved in the previous section. 默认名称为 azuredeploy.jsonThe default name is azuredeploy.json. 必须可以从 shell 访问模板文件。The template file must be accessible from the shell.

    还可以选择使用 ls 命令和 cat 命令来验证文件是否已成功上传。You can optionally use the ls command and the cat command to verify the file is uploaded successfully.

  4. 在 Cloud Shell 中运行以下命令。From the Cloud shell, run the following commands. 选择用于显示 PowerShell 代码或 CLI 代码的选项卡。Select the tab to show the PowerShell code or the CLI code.

    echo "Enter the Resource Group name:" &&
    read resourceGroupName &&
    echo "Enter the location (i.e. centralus):" &&
    read location &&
    az group create --name $resourceGroupName --location "$location" &&
    az group deployment create --resource-group $resourceGroupName --template-file "$HOME/azuredeploy.json"
    

    如果将模板文件保存到了 azuredeploy.json 之外的其他文件中,其更新其名称。Update the template file name if you save the file to a name other than azuredeploy.json.

    以下屏幕截图显示了一个示例部署:The following screenshot shows a sample deployment:

    输出部分的存储帐户名称和存储 URL 在屏幕截图上突出显示。The storage account name and the storage URL in the outputs section are highlighted on the screenshot. 在下一步需要此存储帐户名称。You need the storage account name in the next step.

  5. 运行以下 CLI 或 PowerShell 命令,列出新建的存储帐户:Run the following CLI or PowerShell command to list the newly created storage account:

    echo "Enter the Resource Group name:" &&
    read resourceGroupName &&
    echo "Enter the Storage Account name:" &&
    read storageAccountName &&
    az storage account show --resource-group $resourceGroupName --name $storageAccountName
    

若要详细了解如何使用 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 门户上的左侧菜单中选择“资源组”。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

本快速入门的主要关注点是如何使用 Visual Studio Code 来编辑 Azure 快速入门模板中的现有模板。The main focus of this quickstart is to use Visual Studio Code to edit an existing template from Azure Quickstart templates. 你还学习了如何从 Azure Cloud Shell 中使用 CLI 或 PowerShell 来部署模板。You also learned how to deploy the template using either CLI or PowerShell from the Azure Cloud shell. Azure 快速入门模板中的模板可能并未提供你所需的一切。The templates from Azure Quickstart templates might not give you everything you need. 下一教程介绍如何从模板参考中查找信息,以便创建加密的 Azure 存储帐户。The next tutorial shows you how to find the information from template reference so you can create an encrypted Azure Storage account.