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

通过 Visual Studio 创建和部署 Azure 资源组Creating and deploying Azure resource groups through Visual Studio

使用 Visual Studio 可以创建一个项目,用于将基础结构和代码部署到 Azure。With Visual Studio, you can create a project that deploys your infrastructure and code to Azure. 例如,可以部署 Web 主机、网站和网站代码。For example, you can deploy the web host, website, and code for the website. Visual Studio 许多不同的入门模板用于部署常见方案。Visual Studio provides many different starter templates for deploying common scenarios. 本文中将部署一个 Web 应用。In this article, you deploy a web app.

本文介绍如何使用装有 Azure 开发和 ASP.NET 工作负荷的 Visual Studio 2019 或更高版本This article shows how to use Visual Studio 2019 or later with the Azure development and ASP.NET workloads installed. 如果使用 Visual Studio 2017,体验将大体相同。If you use Visual Studio 2017, your experience is largely the same.

创建 Azure 资源组项目Create Azure Resource Group project

在本部分,我们将使用“Web 应用”模板创建 Azure 资源组项目 。In this section, you create an Azure Resource Group project with a Web app template.

  1. 在 Visual Studio 中,选择“文件”>“新建”>“项目” 。In Visual Studio, choose File>New>Project.

  2. 选择“Azure 资源组”项目模板和“下一步” 。Select the Azure Resource Group project template and Next.

    创建项目

  3. 为你的项目命名。Give your project a name. 其他默认设置可能没问题,但请查看它们,使它们适用于你的环境。The other default settings are probably fine, but review them to make they work for your environment. 完成后,选择“创建”。 When done, select Create.

    创建项目

  4. 选择要部署到 Azure 资源管理器的模板。Choose the template that you want to deploy to Azure Resource Manager. 可以看到,系统根据要部署的项目类型提供了许多不同的选项。Notice there are many different options based on the type of project you wish to deploy. 就本文来说,请选择“Web 应用”模板和“确定” 。For this article, choose the Web app template and OK.

    选择模板

    选择的模板只是起点;可以根据方案添加和删除资源。The template you pick is just a starting point; you can add and remove resources to fulfill your scenario.

  5. Visual Studio 将创建 Web 应用的资源组部署项目。Visual Studio creates a resource group deployment project for the web app. 若要查看项目文件,请查看部署项目中的节点。To see the files for your project, look at the node in the deployment project.

    显示节点

    由于选择了“Web 应用”模板,因此会看到以下文件:Since you chose the Web app template, you see the following files:

    文件名File name 说明Description
    Deploy-AzureResourceGroup.ps1Deploy-AzureResourceGroup.ps1 一个 PowerShell 脚本,运行 PowerShell 命令以部署到 Azure 资源管理器。A PowerShell script that runs PowerShell commands to deploy to Azure Resource Manager. Visual Studio 使用此 PowerShell 脚本来部署模板。Visual Studio uses this PowerShell script to deploy your template.
    WebSite.jsonWebSite.json 资源管理器模板,定义要部署到 Azure 的基础结构,以及在部署期间可以提供的参数。The Resource Manager template that defines the infrastructure you want deploy to Azure, and the parameters you can provide during deployment. 它还定义各资源之间的依赖关系,以便资源管理器按正确的顺序部署资源。It also defines the dependencies between the resources so Resource Manager deploys the resources in the correct order.
    WebSite.parameters.jsonWebSite.parameters.json 包含模板所需值的参数文件。A parameters file that has values needed by the template. 需要传入这些参数值来自定义每个部署。You pass in parameter values to customize each deployment.

    所有资源组部署项目都包含这些基本文件。All resource group deployment projects have these basic files. 其他项目可能包含其他文件以支持其他功能。Other projects may have additional files to support other functionality.

自定义资源管理器模板Customize Resource Manager template

可以通过修改资源管理器模板(描述要部署的资源)来自定义部署项目。You can customize a deployment project by modifying the Resource Manager template that describes the resources you want to deploy. 若要了解资源管理器模板的元素,请参阅创作 Azure 资源管理器模板To learn about the elements of the Resource Manager template, see Authoring Azure Resource Manager templates.

  1. 若要使用模板,请打开“WebSite.json” 。To work on your template, open WebSite.json.

  2. Visual Studio 编辑器提供了工具来帮助编辑资源管理器模板。The Visual Studio editor provides tools to assist you with editing the Resource Manager template. “JSON 大纲”窗口可让你轻松查看模板中定义的元素 。The JSON Outline window makes it easy to see the elements defined in your template.

    显示 JSON 大纲

  3. 选择大纲中的一个元素以转到模板的那个部分。Select an element in the outline to go to that part of the template.

    导航 JSON

  4. 可以通过选择“JSON 大纲”窗口顶部的“添加资源” 按钮,或右键单击“资源” ,并选择“添加新资源” ,来添加资源。You can add a resource by either selecting the Add Resource button at the top of the JSON Outline window, or by right-clicking resources and selecting Add New Resource.

    添加资源

  5. 选择“存储帐户” 并为其命名。Select Storage Account and give it a name. 提供一个名称,该名称不超过 11 个字符,并且只包含数字和小写字母。Provide a name that is no more than 11 characters, and only contains numbers and lower-case letters.

    添加存储

  6. 请注意,不仅会添加资源,而且还添加存储帐户类型的参数,以及存储帐户名称的变量。Notice that not only was the resource added, but also a parameter for the type storage account, and a variable for the name of the storage account.

    显示大纲

  7. 存储帐户类型的参数是使用允许的类型和默认类型预定义的。The parameter for the type of storage account is pre-defined with allowed types and a default type. 可以保留或根据方案编辑这些值。You can leave these values or edit them for your scenario. 如果不希望任何人通过此模板部署 Premium_LRS 存储帐户,请将它从允许的类型中删除。If you don't want anyone to deploy a Premium_LRS storage account through this template, remove it from the allowed types.

    "demoaccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_ZRS",
        "Standard_GRS",
        "Standard_RAGRS"
      ]
    }
    
  8. Visual Studio 还提供 intellisense,帮助你了解在编辑模板时可用的属性。Visual Studio also provides intellisense to help you understand the properties that are available when editing the template. 例如,若要编辑应用服务计划的属性,请导航到 HostingPlan 资源,并为 properties 添加值。For example, to edit the properties for your App Service plan, navigate to the HostingPlan resource, and add a value for the properties. 请注意,Intellisense 显示可用的值,并提供该值的说明。Notice that intellisense shows the available values and provides a description of that value.

    显示 Intellisense

    可以将 numberOfWorkers 设置为 1,并保存文件 。You can set numberOfWorkers to 1, and save the file.

    "properties": {
      "name": "[parameters('hostingPlanName')]",
      "numberOfWorkers": 1
    }
    
  9. 打开 WebSite.parameters.json 文件 。Open the WebSite.parameters.json file. 可以使用参数文件在部署期间传递值,这些值可以自定义正在部署的资源。You use the parameters file to pass in values during deployment that customize the resource being deployed. 给托管计划命名,并保存文件。Give the hosting plan a name, and save the file.

    {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "hostingPlanName": {
          "value": "demoHostPlan"
        }
      }
    }
    

将项目部署到 AzureDeploy project to Azure

现在已准备好将项目部署到资源组。You're now ready to deploy your project to a resource group.

默认情况下,项目中的 PowerShell 脚本 (Deploy-AzureResourceGroup.ps1) 使用 AzureRM 模块。By default, the PowerShell script (Deploy-AzureResourceGroup.ps1) in the project uses the AzureRM module. 如果仍然安装了 AzureRM 模块,并想要继续使用它,可以使用此默认脚本。If you still have the AzureRM module installed and want to continue using it, you can use this default script. 借助此脚本,可以使用 Visual Studio 接口部署解决方案。With this script, you can use the Visual Studio interface to deploy your solution.

但是,如果你已迁移到新的 Az 模块,需要将新脚本添加到项目。However, if you've migrated to the new Az module, you need to add a new script to your project. 若要添加使用 Az 模块的脚本,请复制 Deploy-AzTemplate.ps1 脚本并将其添加到项目。To add a script that uses the Az module, copy the Deploy-AzTemplate.ps1 script and add it to your project. 若要使用此脚本进行部署,必须从 PowerShell 控制台中运行它,而不是使用 Visual Studio 部署接口。To use this script for deployment, you must run it from a PowerShell console, rather than using Visual Studio's deployment interface.

本文对这两种方法都进行了介绍。Both approaches are shown in this article. 本文将默认脚本称为 AzureRM 模块脚本,新脚本称为 Az 模块脚本。This article refers to the default script as the AzureRM module script, and the new script as the Az module script.

Az 模块脚本Az module script

对于 Az 模块脚本,请打开 PowerShell 控制台并运行:For the Az module script, open a PowerShell console and run:

.\Deploy-AzTemplate.ps1 -ArtifactStagingDirectory . -Location centralus -TemplateFile WebSite.json -TemplateParametersFile WebSite.parameters.json

AzureRM 模块脚本AzureRM module script

对于 AzureRM 模块脚本,请使用 Visual Studio:For the AzureRM module script, use Visual Studio:

  1. 在部署项目节点的快捷菜单中,选择“部署” > “新建” 。On the shortcut menu of the deployment project node, choose Deploy > New.

    新建部署菜单项

  2. 此时会显示“部署到资源组”对话框 。The Deploy to Resource Group dialog box appears. 在“资源组” 下拉框中,选择现有资源组或创建新资源组。In the Resource group dropdown box, choose an existing resource group or create a new one. 选择“部署”。 Select Deploy.

    部署到资源组对话框

  3. 在“输出” 窗口中可查看部署状态。In the Output windows, you see the status of the deployment. 完成部署后,最后一条消息指示部署成功,其内容与下面的消息类似:When the deployment has finished, the last message indicates a successful deployment with something similar to:

    18:00:58 - Successfully deployed template 'website.json' to resource group 'ExampleAppDeploy'.
    

查看已部署的资源View deployed resources

让我们检查结果。Let's check the results.

  1. 在浏览器中,打开 Azure 门户并登录到帐户。In a browser, open the Azure portal and sign in to your account. 要查看资源组,请选择“资源组” ,并选择已部署到的资源组。To see the resource group, select Resource groups and the resource group you deployed to.

  2. 将显示所有已部署的资源。You see all the deployed resources. 请注意,存储帐户的名称并不完全是添加资源时指定的名称。Notice that the name of the storage account isn't exactly what you specified when adding that resource. 存储帐户必须是唯一的。The storage account must be unique. 模板自动向所提供的名称添加一个字符串,以便创建唯一名称。The template automatically adds a string of characters to the name you provided to create a unique name.

    显示资源

将代码添加到项目Add code to project

此时,已为应用部署基础结构,但尚未在项目中部署实际代码。At this point, you've deployed the infrastructure for your app, but there's no actual code deployed with the project.

  1. 将项目添加到 Visual Studio 解决方案。Add a project to your Visual Studio solution. 右键单击解决方案,选择“添加” > “新建项目” 。Right-click the solution, and select Add > New Project.

    添加项目

  2. 添加“ASP.NET Core Web 应用程序” 。Add an ASP.NET Core Web Application.

    添加 Web 应用

  3. 为你的 web 应用命名,然后选择“创建” 。Give your web app a name, and select Create.

    为 web 应用命名

  4. 选择“Web 应用程序”和“创建” 。Select Web Application and Create.

    选择 Web 应用程序

  5. 在 Visual Studio 创建 Web 应用之后,可在解决方案中看到这两个项目。After Visual Studio creates your web app, you see both projects in the solution.

    显示项目

  6. 现在,需要确保资源组项目与此新项目之间建立链接。Now, you need to make sure your resource group project is aware of the new project. 返回到资源组项目 (ExampleAppDeploy)。Go back to your resource group project (ExampleAppDeploy). 右键单击“引用” ,选择“添加引用” 。Right-click References and select Add Reference.

    添加引用

  7. 选择创建的 Web 应用项目。Select the web app project that you created.

    添加引用

    通过添加引用,可以将 Web 应用项目链接到资源组项目中,并自动设置一些属性。By adding a reference, you link the web app project to the resource group project, and automatically sets some properties. 可以在“属性” 窗口中看到针对该引用的属性。You see these properties in the Properties window for the reference. “包含文件路径” 包含创建包所在的路径。The Include File Path has the path where the package is created. 请记下文件夹 (ExampleApp) 和文件 (package.zip)。Note the folder (ExampleApp) and file (package.zip). 用户需要知道这些值,因为在部署应用时需提供这些值作为参数。You need to know these values because you provide them as parameters when deploying the app.

    查看引用

  8. 返回到你的模板 (WebSite.json),向模板添加资源。Go back to your template (WebSite.json) and add a resource to the template.

    添加资源

  9. 这次选择“用于 Web 应用的 Web 部署” 。This time select Web Deploy for Web Apps.

    添加 Web 部署

    保存模板。Save your template.

  10. 你的模板中有一些新的参数。There are some new parameters in your template. 它们是在先前的步骤中添加进来的。They were added in the previous step. 不需要为 _artifactsLocation 或 _artifactsLocationSasToken 提供值,因为这些值自动生成 。You don't need to provide values for _artifactsLocation or _artifactsLocationSasToken because those values are automatically generated. 但是,必须将文件夹和文件名称设置为包含部署包的路径。However, you have to set the folder and file name to the path that contains the deployment package. 这些参数的名称以 PackageFolder 和 PackageFileName 结尾 。The names of these parameters end with PackageFolder and PackageFileName. 名称的第一部分是添加的 Web 部署资源的名称。The first part of the name is the name of the Web Deploy resource you added. 在本文中,它们被命名为 ExampleAppPackageFolder 和 ExampleAppPackageFileName 。In this article, they're named ExampleAppPackageFolder and ExampleAppPackageFileName.

    打开 Website.parameters.json 并将这些参数设置为你在引用属性中看到的值 。Open Website.parameters.json and set those parameters to the values you saw in the reference properties. 将 ExampleAppPackageFolder 设置为文件夹的名称 。Set ExampleAppPackageFolder to the name of the folder. 将 ExampleAppPackageFileName 设置为 zip 文件的名称 。Set ExampleAppPackageFileName to the name of the zip file.

    {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "hostingPlanName": {
          "value": "demoHostPlan"
        },
        "ExampleAppPackageFolder": {
          "value": "ExampleApp"
        },
        "ExampleAppPackageFileName": {
          "value": "package.zip"
        }
      }
    }
    

将代码与基础结构一起部署Deploy code with infrastructure

因为你将代码添加到了项目,所以这次部署会稍有不同。Because you added code to the project, your deployment is a little different this time. 在部署期间,将项目的生成工件暂存到资源管理器可以访问的位置。During deployment, you stage artifacts for your project to a place that Resource Manager can access. 生成工件会被暂存到存储帐户。The artifacts are staged to a storage account.

Az 模块脚本Az module script

如果你正在使用 Az 模块脚本,那么需要对模板做一个小的更改。There's one small change you need to make to your template if you're using the Az module script. 此脚本将斜杠添加到生成工件位置,但模板并不需要该斜杠。This script adds a slash to the artifacts location but your template doesn't expect that slash. 打开 WebSite.json 并找到 MSDeploy 扩展的属性。Open WebSite.json and find the properties for the MSDeploy extension. 它有一个名为 packageUri 的属性 。It has a property named packageUri. 删除生成工件位置和包文件夹之间的斜杠。Remove the slash between the artifacts location and the package folder.

它看起来应该如下所示:It should look like:

"packageUri": "[concat(parameters('_artifactsLocation'), parameters('ExampleAppPackageFolder'), '/', parameters('ExampleAppPackageFileName'), parameters('_artifactsLocationSasToken'))]",

请注意,在前面的示例中,在 parameters('_artifactsLocation') 和 parameters('ExampleAppPackageFolder') 之间没有 '/',Notice in the preceding example there is no '/', between parameters('_artifactsLocation') and parameters('ExampleAppPackageFolder').

重新生成项目。Rebuild the project. 生成项目可确保需部署的文件添加到暂存文件夹中。Building the project makes sure the files you need to deploy are added to the staging folder.

现在,打开 PowerShell 控制台并运行:Now, open a PowerShell console and run:

.\Deploy-AzTemplate.ps1 -ArtifactStagingDirectory .\bin\Debug\staging\ExampleAppDeploy -Location centralus -TemplateFile WebSite.json -TemplateParametersFile WebSite.parameters.json -UploadArtifacts -StorageAccountName <storage-account-name>

AzureRM 模块脚本AzureRM module script

对于 AzureRM 模块脚本,请使用 Visual Studio:For the AzureRM module script, use Visual Studio:

  1. 若要重新部署,请选择“部署” ,以及以前部署的资源组。To redeploy, choose Deploy, and the resource group you deployed earlier.

    重新部署项目

  2. 为“生成工件存储帐户”选择部署此资源组时所用的存储帐户 。Select the storage account you deployed with this resource group for the Artifact storage account.

    重新部署 web 部署

查看 Web 应用View web app

  1. 部署完毕后,请在门户中选择 Web 应用。After the deployment has finished, select your web app in the portal. 选择 URL 以浏览到此站点。Select the URL to browse to the site.

    浏览站点

  2. 请注意已成功部署默认的 ASP.NET 应用程序。Notice that you've successfully deployed the default ASP.NET app.

    显示已部署的应用

添加操作仪表板Add operations dashboard

并不仅限于通过 Visual Studio 界面提供的资源。You aren't limited to only the resources that are available through the Visual Studio interface. 可将自定义资源添加到模板来自定义部署。You can customize your deployment by adding a custom resource to your template. 若要显示如何添加资源,请添加一个操作仪表板来管理部署的资源。To show adding a resource, you add an operational dashboard to manage the resource you deployed.

  1. 打开 WebSite.json 文件,在存储帐户资源后但在资源部分的右 ] 前添加以下 JSON。Open the WebSite.json file and add the following JSON after the storage account resource but before the closing ] of the resources section.

     ,{
       "properties": {
         "lenses": {
           "0": {
             "order": 0,
             "parts": {
               "0": {
                 "position": {
                   "x": 0,
                   "y": 0,
                   "colSpan": 4,
                   "rowSpan": 6
                 },
                 "metadata": {
                   "inputs": [
                     {
                       "name": "resourceGroup",
                       "isOptional": true
                     },
                     {
                       "name": "id",
                       "value": "[resourceGroup().id]",
                       "isOptional": true
                     }
                   ],
                   "type": "Extension/HubsExtension/PartType/ResourceGroupMapPinnedPart"
                 }
               },
               "1": {
                 "position": {
                   "x": 4,
                   "y": 0,
                   "rowSpan": 3,
                   "colSpan": 4
                 },
                 "metadata": {
                   "inputs": [],
                   "type": "Extension[azure]/HubsExtension/PartType/MarkdownPart",
                   "settings": {
                     "content": {
                       "settings": {
                         "content": "__Customizations__\n\nUse this dashboard to create and share the operational views of services critical to the application performing. To customize simply pin components to the dashboard and then publish when you're done. Others will see your changes when you publish and share the dashboard.\n\nYou can customize this text too. It supports plain text, __Markdown__, and even limited HTML like images <img width='10' src='https://portal.azure.com/favicon.ico'/> and <a href='https://azure.microsoft.com' target='_blank'>links</a> that open in a new tab.\n",
                         "title": "Operations",
                         "subtitle": "[resourceGroup().name]"
                       }
                     }
                   }
                 }
               }
             }
           }
         },
         "metadata": {
           "model": {
             "timeRange": {
               "value": {
                 "relative": {
                   "duration": 24,
                   "timeUnit": 1
                 }
               },
               "type": "MsPortalFx.Composition.Configuration.ValueTypes.TimeRange"
             }
           }
         }
       },
       "apiVersion": "2015-08-01-preview",
       "name": "[concat('ARM-',resourceGroup().name)]",
       "type": "Microsoft.Portal/dashboards",
       "location": "[resourceGroup().location]",
       "tags": {
         "hidden-title": "[concat('OPS-',resourceGroup().name)]"
       }
     }
    
  2. 重新部署项目。Redeploy your project.

  3. 部署完成后,请在门户中查看你的仪表板。After deployment has finished, view your dashboard in the portal. 选择“仪表板” 并选择你部署的仪表板。Select Dashboard and pick the one you deployed.

    自定义仪表板

  4. 可看到自定义的仪表板。You see the customized dashboard.

    自定义仪表板

可以使用 RBAC 组管理对仪表板的访问。You can manage access to the dashboard by using RBAC groups. 部署后,还可以自定义仪表板的外观。You can also customize the dashboard's appearance after it's deployed. 但是,如果重新部署资源组,则模板中的仪表板将重置为其默认状态。However, if you redeploy the resource group, the dashboard is reset to its default state in your template. 有关创建仪表板的详细信息,请参阅以编程方式创建 Azure 仪表板For more information about creating dashboards, see Programmatically create Azure Dashboards.

清理资源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 groups from the left menu.

  2. 选择资源组名称。Select the resource group name.

  3. 在顶部菜单中选择“删除资源组”。 Select Delete resource group from the top menu.

后续步骤Next steps

在此快速入门中,你了解了如何使用 Visual Studio 创建和部署模板。In this quickstart, you learned how to create and deploy templates using Visual Studio. 若要详细了解模板开发,请参阅新的初学者教程系列:To learn more about template development, see our new beginner tutorial series: