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

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

使用 Visual Studio 和 Azure SDK可以创建一个项目,用于将基础结构和代码部署到 Azure。With Visual Studio and the Azure SDK, you can create a project that deploys your infrastructure and code to Azure. 例如,可以为应用定义 Web 主机、网站和数据库,并将该基础结构与代码一起部署。For example, you can define the web host, web site, and database for your app, and deploy that infrastructure along with the code. 或者,可以定义虚拟机、虚拟网络和存储帐户,并连同虚拟机上执行的脚本一起部署该基础结构。Or, you can define a Virtual Machine, Virtual Network and Storage Account, and deploy that infrastructure along with a script that is executed on Virtual Machine. 使用 Azure 资源组部署项目,只需单个可重复的操作即可部署所有需要的资源。The Azure Resource Group deployment project enables you to deploy all the needed resources in a single, repeatable operation. 有关部署和管理资源的详细信息,请参阅 Azure 资源管理器概述For more information about deploying and managing your resources, see Azure Resource Manager overview.

Azure 资源组项目包含 Azure 资源管理器 JSON 模板,用于定义部署到 Azure 的资源。Azure Resource Group projects contain Azure Resource Manager JSON templates, which define the resources that you deploy to Azure. 若要了解资源管理器模板的元素,请参阅创作 Azure 资源管理器模板To learn about the elements of the Resource Manager template, see Authoring Azure Resource Manager templates. Visual Studio 允许编辑这些模板,并提供工具来简化模板的使用。Visual Studio enables you to edit these templates, and provides tools that simplify working with templates.

本文部署 Web 应用和 SQL 数据库。In this article, you deploy a web app and SQL Database. 但是,任何类型的资源的部署步骤几乎是相同的。However, the steps are almost the same for any type resource. 可以轻松地部署虚拟机及其相关资源。You can as easily deploy a Virtual Machine and its related resources. Visual Studio 许多不同的入门模板用于部署常见方案。Visual Studio provides many different starter templates for deploying common scenarios.

本文介绍 Visual Studio 2017。This article shows Visual Studio 2017. 如果使用 Visual Studio 2015 Update 2 以及用于 .NET 2.9 的 Microsoft Azure SDK,或者将 Visual Studio 2013 与 Azure SDK 2.9 配合使用,则体验大致相同。If you use Visual Studio 2015 Update 2 and Microsoft Azure SDK for .NET 2.9, or Visual Studio 2013 with Azure SDK 2.9, your experience is largely the same. 可以使用 2.6 或更高版本的 Azure SDK;但是,用户界面体验可能会不同于本文所示的用户界面。You can use versions of the Azure SDK from 2.6 or later; however, your experience of the user interface may be different than the user interface shown in this article. 强烈建议先安装最新版本的 Azure SDK ,再开始执行以下步骤。We strongly recommend that you install the latest version of the Azure SDK before starting the steps.

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

在此过程中,使用“Web 应用 + SQL”模板创建 Azure 资源组项目 。In this procedure, you create an Azure Resource Group project with a Web app + SQL template.

  1. 在 Visual Studio 中,依次选择“文件”、“新建项目”,选择 C#Visual Basic(选择哪种语言对以后的阶段没有任何影响,因为这些项目仅包含 JSON 和 PowerShell 的内容)。In Visual Studio, choose File, New Project, choose either C# or Visual Basic (which language you choose has no impact on the later stages as these projects contain only JSON and PowerShell content). 然后选择“云”和“Azure 资源组”项目。Then choose Cloud, and Azure Resource Group project.

    云部署项目

  2. 选择要部署到 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 应用 + SQL”模板。For this article, choose the Web app + SQL template.

    选择模板

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

    备注

    Visual Studio 会在线检索可用模板的列表。Visual Studio retrieves a list of available templates online. 该列表可能会更改。The list may change.

    Visual Studio 将创建 Web 应用和 SQL 数据库的资源组部署项目。Visual Studio creates a resource group deployment project for the web app and SQL database.

  3. 若要查看创建的内容,请浏览部署项目中的节点。To see what you created, look at the node in the deployment project.

    显示节点

    由于针对本示例选择了“Web 应用 + SQL”模板,因此会看到以下文件。Since we chose the Web app + SQL template for this example, you see the following files:

    文件名File name 说明Description
    Deploy-AzureResourceGroup.ps1Deploy-AzureResourceGroup.ps1 一个 PowerShell 脚本,调用 PowerShell 命令以部署到 Azure 资源管理器。A PowerShell script that invokes PowerShell commands to deploy to Azure Resource Manager.
    请注意 Visual Studio 使用此 PowerShell 脚本来部署模板。Note Visual Studio uses this PowerShell script to deploy your template. 对此脚本进行任何更改会影响 Visual Studio 中的部署,因此请务必小心。Any changes you make to this script affect deployment in Visual Studio, so be careful.
    WebSiteSQLDatabase.jsonWebSiteSQLDatabase.json Resource Manager 模板,定义要部署到 Azure 的基础结构,以及在部署期间可以提供的参数。The Resource Manager template that defines the infrastructure you want deploy to Azure, and the parameters you can provide during deployment. 它还定义各资源之间的依赖关系,以便 Resource Manager 按正确的顺序部署资源。It also defines the dependencies between the resources so Resource Manager deploys the resources in the correct order.
    WebSiteSQLDatabase.parameters.jsonWebSiteSQLDatabase.parameters.json 包含模板所需值的参数文件。A parameters file that contains values needed by the template. 需要传入这些参数值来自定义每个部署。You pass in parameter values to customize each deployment.

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

自定义 Resource Manager 模板Customize the Resource Manager template

可以通过修改 JSON 模板(描述要部署的资源)来自定义部署项目。You can customize a deployment project by modifying the JSON templates that describe the resources you want to deploy. JSON 是“JavaScript 对象表示法”的缩写,是一种易于使用的序列化数据格式。JSON stands for JavaScript Object Notation, and is a serialized data format that is easy to work with. JSON 文件使用在每个文件顶部引用的架构。The JSON files use a schema that you reference at the top of each file. 如果想要了解该架构,可以下载并分析它。If you want to understand the schema, you can download and analyze it. 架构定义所允许的元素、字段的类型和格式、可能的枚举值,等等。The schema defines what elements are valid, the types and formats of fields, the possible values of enumerated values, and so on. 若要了解资源管理器模板的元素,请参阅创作 Azure 资源管理器模板To learn about the elements of the Resource Manager template, see Authoring Azure Resource Manager templates.

若要使用模板,请打开 WebSiteSQLDatabase.jsonTo work on your template, open WebSiteSQLDatabase.json.

Visual Studio 编辑器提供了工具来帮助编辑 Resource Manager 模板。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 大纲

在大纲中选择任一元素会转到模板的该部分,并且突出显示相应的 JSON。Selecting any of the elements in the outline takes you to that part of the template and highlights the corresponding JSON.

导航 JSON

可以通过选择“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.

添加资源

对于本教程,请选择“存储帐户”并指定其名称 。For this tutorial, 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.

添加存储

请注意,不仅会添加资源,而且还添加存储帐户类型的参数,以及存储帐户名称的变量。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.

显示大纲

storageType 参数是使用允许的类型和默认类型预定义的。The storageType parameter 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 do not want anyone to deploy a Premium_LRS storage account through this template, remove it from the allowed types.

"storageType": {
  "type": "string",
  "defaultValue": "Standard_LRS",
  "allowedValues": [
    "Standard_LRS",
    "Standard_ZRS",
    "Standard_GRS",
    "Standard_RAGRS"
  ]
}

Visual Studio 还提供 intellisense,帮助你了解在编辑模板时哪些属性可用。Visual Studio also provides intellisense to help you understand what properties 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.

"properties": {
  "name": "[parameters('hostingPlanName')]",
  "numberOfWorkers": 1
}

将资源组部署到 AzureDeploy the Resource Group project to Azure

现在已准备好部署项目。You are now ready to deploy your project. 部署 Azure 资源组项目时,请将其部署到 Azure 资源组。When you deploy an Azure Resource Group project, you deploy it to an Azure resource group. 资源组是共享同一生命周期的资源的逻辑分组。The resource group is a logical grouping of resources that share a common lifecycle.

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

    “部署”>“新建部署”菜单项

    此时会显示“部署到资源组”对话框。The Deploy to Resource Group dialog box appears.

    部署到资源组对话框

  2. 在“资源组”下拉框中,选择现有资源组或创建新资源组。In the Resource group dropdown box, choose an existing resource group or create a new one. 要创建资源组,请打开“资源组”下拉框,并选择“新建”。To create a resource group, open the Resource Group dropdown box and choose Create New.

    部署到资源组对话框

    此时会显示“创建资源组”对话框。The Create Resource Group dialog box appears. 指定组的名称和位置,并选择“创建”按钮。Give your group a name and location, and select the Create button.

    创建资源组对话框

  3. 选择“编辑参数”按钮以编辑部署的参数。Edit the parameters for the deployment by selecting the Edit Parameters button.

    编辑参数按钮

  4. 提供空值参数的值,并选择“保存”按钮。Provide values for the empty parameters and select the Save button. 空值参数为 hostingPlanNameadministratorLoginadministratorLoginPassworddatabaseNameThe empty parameters are hostingPlanName, administratorLogin, administratorLoginPassword, and databaseName.

    hostingPlanName 指定要创建的应用服务计划的名称。hostingPlanName specifies a name for the App Service plan to create.

    administratorLogin 指定 SQL Server 管理员的用户名。administratorLogin specifies the user name for the SQL Server administrator. 请勿使用常用的管理员名称,如 saadminDo not use common admin names like sa or admin.

    administratorLoginPassword 指定 SQL Server 管理员的密码。The administratorLoginPassword specifies a password for SQL Server administrator. “在参数文件中以纯文本格式保存密码”选项不安全;所以,请勿选择此选项。The Save passwords as plain text in the parameters file option is not secure; therefore, do not select this option. 由于不以纯文本格式保存密码,因此在部署过程中需要再次提供此密码。Since the password is not saved as plain text, you need to provide this password again during deployment.

    databaseName 指定要创建的数据库的名称。databaseName specifies a name for the database to create.

    编辑参数对话框

  5. 选择“部署”按钮将项目部署到 Azure。Choose the Deploy button to deploy the project to Azure. PowerShell 控制台会在 Visual Studio 实例外部打开。A PowerShell console opens outside of the Visual Studio instance. 出现密码输入提示时,在 PowerShell 控制台中输入 SQL Server 管理员密码。Enter the SQL Server administrator password in the PowerShell console when prompted. PowerShell 控制台可能隐藏在其他项目后面或最小化到任务栏。Your PowerShell console may be hidden behind other items or minimized in the task bar. 查找此控制台,选择它以提供密码。Look for this console and select it to provide the password.

    备注

    Visual Studio 可能会要求安装 Azure PowerShell cmdlet。Visual Studio may ask you to install the Azure PowerShell cmdlets. 需要安装 Azure PowerShell cmdlet 才能成功部署资源组。You need the Azure PowerShell cmdlets to successfully deploy resource groups. 如果出现提示,请安装 Azure PowerShell cmdlet。If prompted, install them. 有关详细信息,请参阅安装和配置 Azure PowerShellFor more information, see Install and configure Azure PowerShell.

  6. 该部署可能需要几分钟时间。The deployment may take a few minutes. 在“输出”窗口中可查看部署状态。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 'websitesqldatabase.json' to resource group 'DemoSiteGroup'.
    
  7. 在浏览器中,打开 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.

    选择组

  8. 将显示所有已部署的资源。You see all the deployed resources. 请注意,存储帐户的名称并不完全是添加资源时指定的名称。Notice that the name of the storage account is not 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 provide a unique name.

    显示资源

  9. 如果做了更改并想要重新部署项目,可以从 Azure 资源组项目的快捷菜单中选择现有资源组。If you make changes and want to redeploy your project, choose the existing resource group from the shortcut menu of Azure resource group project. 在快捷菜单中,选择“部署”,并选择已部署的资源组。On the shortcut menu, choose Deploy, and then choose the resource group you deployed.

    Azure 资源组已部署

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

此时,已为应用部署基础结构,但尚未在项目中部署实际代码。At this point, you have deployed the infrastructure for your app, but there is no actual code deployed with the project. 本文说明如何在部署期间部署 Web 应用和 SQL 数据库表。This article shows how to deploy a web app and SQL Database tables during deployment. 如果是部署虚拟机而不是 Web 应用,需要在部署过程中,在计算机上运行一些代码。If you are deploying a Virtual Machine instead of a web app, you want to run some code on the machine as part of deployment. 为 Web 应用部署代码的过程与设置虚拟机的过程几乎相同。The process for deploying code for a web app or for setting up a Virtual Machine is almost the same.

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

    添加项目

  2. 添加 ASP.NET Web 应用Add an ASP.NET Web Application.

    添加 Web 应用

  3. 选择 MVCSelect MVC.

    选择 MVC

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

    显示项目

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

    添加引用

  6. 选择创建的 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 set three key properties. 可以在“属性”窗口中看到针对该引用的属性。You see these properties in the Properties window for the reference.

    查看引用

    属性包括:The properties are:

    • “其他属性”包含要推送到 Azure 存储的 Web 部署包暂存位置。The Additional Properties contains the web deployment package staging location that is pushed to the Azure Storage. 请记下文件夹 (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.
    • “包含文件路径”包含创建包所在的路径。The Include File Path contains the path where the package is created. “包含目标”包含部署执行的命令。The Include Targets contains the command that deployment executes.
    • 默认值“生成并打包”可让部署生成并创建 Web 部署包 (package.zip) 。The default value of Build;Package enables the deployment to build and create a web deployment package (package.zip).

      不需要使用发布配置文件,因为部署将从属性中获取所需的信息来创建包。You do not need a publish profile as the deployment gets the necessary information from the properties to create the package.

  7. 返回到 WebSiteSQLDatabase.json,向模板添加资源。Go back to WebSiteSQLDatabase.json and add a resource to the template.

    添加资源

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

    添加 Web 部署

  9. 将资源组项目重新部署到资源组。Redeploy your resource group project to the resource group. 这次还有一些新的参数。This time there are some new parameters. 不需要为 _artifactsLocation_artifactsLocationSasToken 提供值,因为 Visual Studio 会自动生成这些值。You do not need to provide values for _artifactsLocation or _artifactsLocationSasToken because Visual Studio automatically generates those values. 但是,必须将文件夹和文件名称设置为包含部署包的路径(在下图中显示为 ExampleAppPackageFolderExampleAppPackageFileName)。However, you have to set the folder and file name to the path that contains the deployment package (shown as ExampleAppPackageFolder and ExampleAppPackageFileName in the following image). 提供之前在引用属性中看到的值(ExampleApppackage.zip)。Provide the values you saw earlier in the reference properties (ExampleApp and package.zip).

    添加 Web 部署

    对于“项目存储帐户”,选择部署此资源组时所用的帐户 。For the Artifact storage account, select the one deployed with this resource group.

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

    浏览站点

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

    显示已部署的应用

将操作仪表板添加到部署Add an operations dashboard to your deployment

现在,我们已创建解决方案,可以到最后阶段使其可操作了。Now that we've created a solution it's time to go the last mile and make it operational. 并不仅限于通过 Visual Studio 界面提供的资源。You are not limited to only the resources that are available through the Visual Studio interface. 我们可以利用共享仪表板,它们以 JSON 格式定义为资源。We can leverage the use of shared dashboards, which are defined as resources in JSON. 可以通过编辑模板和添加自定义资源来执行此操作。We do this by editing our template and adding a custom resource.

  1. 打开 WebsiteSqlDeploy.json 文件,在存储帐户资源后但在资源节的右 ] 前添加以下 json 代码块。Open the WebsiteSqlDeploy.json file and add the following json block of code 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)]"
      }
    }
}
  1. 重新部署资源组,然后在 Azure 门户中查看仪表板时,将看到已添加到所选列表的共享仪表板。Redeploy your resource group and when you look at your dashboard on the azure portal you will see the shared dashboard added to your list of choices.

    自定义仪表板

备注

可以使用 RBAC 组管理对仪表板的访问权限,部署资源后可以将自定义项发布到资源。Access to the dashboard can be managed using RBAC groups and customizations can be published to the resource after its deployed. 请注意,重新部署资源组时,会将其重置回模板中的默认值。Note that when you redploy the resource group it will reset it back to the default in your template. 应考虑使用自定义项更新模板。You should consider updating the template with the customizations. 有关如何执行此操作的帮助,请参阅以编程方式创建 Azure 仪表板For help on how to do this refer to Programmatically create Azure Dashboards

![自定义仪表板](./media/vs-azure-tools-resource-groups-deployment-projects-create-deploy/Ops-DemoSiteGroup-dashboard.png)

后续步骤Next steps