练习 - 授权服务主体进行部署

已完成

在上一个练习中,你为网站的部署管道创建了一个服务主体,还测试了你可使用其密钥进行登录。 现在,你可为 Azure 环境授予服务主体访问权限。 在此练习中,你将为服务主体创建角色分配,然后使用服务主体部署 Bicep 文件。

在此过程中,你将:

  • 使用自己的用户帐户登录。
  • 为公司的网站创建一个资源组。
  • 创建角色分配,以允许服务主体将资源部署到资源组。
  • 以服务主体的身份登录,并部署用于创建公司网站资源的 Bicep 文件。
  • 验证部署。
  • 清理资源组和服务主体。

使用你的用户帐户登录

在上一个练习中,你使用服务主体进行了登录,然后注销。你需要再次使用自己的用户帐户登录,来执行本练习的后续步骤。

  1. 在 Visual Studio Code 的 Azure Cloud Shell (bash) 终端中,运行以下命令登录到 Azure:

    az login
    
  2. 在打开的浏览器中,登录到 Azure 帐户。

  1. 在 Visual Studio Code 的 Azure Cloud Shell (PowerShell) 终端中,运行以下命令登录到 Azure:

    Connect-AzAccount
    
  2. 在打开的浏览器中,登录到 Azure 帐户。

创建资源组

现在你将创建一个资源组,用于包含玩具公司的网站资源。

  1. 在 Visual Studio Code 终端中运行此 Azure CLI 命令,创建一个资源组:

    az group create --name ToyWebsite --location eastus
    
  2. 查看上一个命令的 JSON 输出。 它包含一个名为 id 属性,即资源组的 ID。 将其复制到安全位置。 稍后将会用到。

  1. 在 Visual Studio Code 终端中运行此 Azure PowerShell 命令,创建一个资源组:

    New-AzResourceGroup -Name ToyWebsite -Location eastus
    
  2. 查看上一个命令的输出。 它包含一个名为 ResourceId 属性,即资源组的 ID。 将其复制到安全位置。 稍后将会用到。

创建角色分配

对于网站的部署管道,你决定创建包含以下详细信息的角色分配:

  • 被分派人:在上一个练习中创建的服务主体。
  • 角色:内置角色“参与者”。
  • 范围:前述步骤中创建的资源组。

在 Visual Studio Code 终端中运行以下 Azure CLI 命令来创建角色分配。 将占位符替换为之前复制的值。

az role assignment create \
  --assignee APPLICATION_ID \
  --role Contributor \
  --scope RESOURCE_GROUP_ID \
  --description "The deployment pipeline for the company's website needs to be able to create resources within the resource group."

在 Visual Studio Code 终端中运行以下 Azure CLI 命令来创建角色分配。 将占位符替换为之前复制的值。

New-AzRoleAssignment `
  -ApplicationId APPLICATION_ID `
  -RoleDefinitionName Contributor `
  -Scope RESOURCE_GROUP_ID `
  -Description "The deployment pipeline for the company's website needs to be able to create resources within the resource group."

创建 Bicep 文件

你之前创建了一个 Bicep 文件来部署网站资源。 在这里保存该文件,以便你可使用服务主体对其进行测试。

  1. 新建一个名为 main.bicep 的文件。

  2. 将以下内容添加到 main.bicep 文件。 你很快就会部署模板。

    @description('The Azure region into which the resources should be deployed.')
    param location string = resourceGroup().location
    
    @description('The name of the App Service app.')
    param appServiceAppName string = 'toywebsite${uniqueString(resourceGroup().id)}'
    
    @description('The name of the App Service plan SKU.')
    param appServicePlanSkuName string = 'F1'
    
    var appServicePlanName = 'toy-website-plan'
    var applicationInsightsInstanceName = 'toy-website-insights'
    
    resource appServicePlan 'Microsoft.Web/serverFarms@2020-06-01' = {
      name: appServicePlanName
      location: location
      sku: {
        name: appServicePlanSkuName
      }
    }
    
    resource applicationInsightsInstance 'Microsoft.Insights/components@2018-05-01-preview' = {
      name: applicationInsightsInstanceName
      location: location
      kind: 'web'
      properties: {
        Application_Type: 'web'
      }
    }
    
    resource appServiceApp 'Microsoft.Web/sites@2020-06-01' = {
      name: appServiceAppName
      location: location
      properties: {
        serverFarmId: appServicePlan.id
        httpsOnly: true
        siteConfig: {
          appSettings: [
            {
              name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
              value: applicationInsightsInstance.properties.InstrumentationKey
            }
            {
              name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
              value: applicationInsightsInstance.properties.ConnectionString
            }
          ]
        }
      }
    }
    
  3. 保存对文件所做的更改。 你可以选择“文件”>“另存为”,也可以在 Windows 中选择Ctrl+S(在 macOS 上选择 ⌘+S)。 请务必记住保存文件的位置。 例如,你可能希望创建脚本文件,并将其存储在其中。

使用服务主体部署 Bicep 文件

你目前没有部署管道,因此将模拟管道部署 Bicep 文件所执行的操作。

  1. 在 Visual Studio Code 终端中运行以下 Azure CLI 命令,使用服务主体的凭据进行登录。 将占位符替换为你在上一个练习中复制的值。

    az login --service-principal \
      --username APPLICATION_ID \
      --password SERVICE_PRINCIPAL_KEY \
      --tenant TENANT_ID
    
  2. 运行以下 Azure CLI 命令来部署 Bicep 文件:

    az deployment group create \
      --resource-group ToyWebsite \
      --template-file main.bicep
    

    此部署可能需要一两分钟才能完成,然后你会看到部署成功。

  1. 在 Visual Studio Code 终端中运行以下 Azure PowerShell 命令,以安全地提示你输入服务主体的凭据。 使用上一练习中的服务主体的应用程序 ID 和密钥分别作为用户名和密码。

    $credential = Get-Credential
    
  2. 在 Visual Studio Code 终端中运行以下 Azure PowerShell 命令,使用服务主体的凭据进行登录。 将 TENANT_ID 占位符替换为之前复制的值。

    Connect-AzAccount -ServicePrincipal `
      -Credential $credential `
      -Tenant TENANT_ID
    
  3. 运行以下 Azure PowerShell 命令来部署 Bicep 文件:

    New-AzResourceGroupDeployment -ResourceGroupName ToyWebsite -TemplateFile main.bicep
    

    此部署可能需要一两分钟才能完成,然后你会看到部署成功。

验证部署

使用 Azure 门户检查部署的资源,并检查部署的结果。

  1. 转到 Azure 门户

  2. 在左窗格中,选择“资源组”。

  3. 选择“ToyWebsite”。

  4. 选择“部署”以查看成功的部署。

    Screenshot of the Azure portal resource group Deployments pane, displaying the successful deployment.

    你还可能会看到名为“Failure-Anomalies-Alert-Rule-Deployment”的部署。 Application Insights 会自动创建此部署。

  5. 选择“主”部署以查看部署了哪些资源,然后展开“部署详细信息”

    这样的话,将列出应用服务计划、应用和 Application Insights 实例。

    Screenshot of the Azure portal deployments overview pane for the main deployment, with an App Service plan and app, and an Application Insights instance listed.

    请注意,此部署没有任何异常。 即使是服务主体启动的,它也与其他任何 Bicep 部署一样。

清理资源组和服务主体

你已成功创建服务主体和角色分配,并且使用 Bicep 文件部署了网站的资源。 现在可删除所创建的资源。

  1. 使用以下命令注销服务主体的帐户:

    az logout
    
  2. 运行以下命令,使用自己的用户帐户重新登录到 Azure:

    az login
    
  3. 在打开的浏览器中,登录到 Azure 帐户。

  4. 运行以下 Azure CLI 命令来删除资源组、其内容和角色分配:

    az group delete --name ToyWebsite
    

    当系统提示确认时,请输入 y

  5. 运行以下命令来删除服务主体。 将 APPLICATION_ID 占位符替换为在上一练习中复制的应用程序 ID:

    az ad sp delete --id APPLICATION_ID
    
  1. 使用以下命令注销服务主体的帐户:

    Logout-AzAccount
    
  2. 运行以下命令,使用自己的用户帐户重新登录到 Azure:

    Connect-AzAccount
    
  3. 在打开的浏览器中,登录到 Azure 帐户。

  4. 运行以下 Azure PowerShell 命令来删除资源组、其内容和角色分配:

    Remove-AzResourceGroup -Name ToyWebsite
    

    当系统提示确认时,请输入 y

  5. 运行以下命令来删除服务主体。 将 APPLICATION_ID 占位符替换为在上一练习中复制的应用程序 ID:

    Remove-AzADServicePrincipal -ApplicationId APPLICATION_ID
    

    当系统提示确认时,请输入 y