练习 - 创建和使用模块

已完成

你的任务是向公司网站添加内容分发网络 (CDN),以便发布玩具袋熊。 但是,公司中的其他团队已经告诉你不需要 CDN。 在此练习中,你将为网站和 CDN 创建模块,并将这些模块添加到一个模板中。

在此过程中,你将:

  • 为应用程序添加一个模块。
  • 创建使用该模块的 Bicep 模板。
  • 为 CDN 添加另一个模块。
  • 将 CDN 模块添加到模板,并使其成为可选。
  • 将模板部署到 Azure。
  • 查看部署历史记录。

本练习使用适用于 Visual Studio Code 的 Bicep 扩展。 请务必在 Visual Studio Code 中安装此扩展。

创建空白 Bicep 文件

  1. 打开 Visual Studio Code。

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

  3. 保存空文件,以便 Visual Studio Code 加载 Bicep 工具。

    你可以选择“文件”>“另存为”,也可以在 Windows 上选择 Ctrl+S(在 macOS 上则选择 ⌘+S)。 请务必记住保存文件的位置。 例如,你需要创建一个“模板”文件夹来将其存储在其中。

为应用程序创建一个模块

  1. 在你创建 main.bicep 文件的同一文件夹中,创建一个名为“模块”的新文件夹。 在“模块”文件夹中,创建一个名为 app.bicep 的文件。 保存文件。

  2. 将以下内容添加到 app.bicep 文件中:

    @description('The Azure region into which the resources should be deployed.')
    param location string
    
    @description('The name of the App Service app.')
    param appServiceAppName string
    
    @description('The name of the App Service plan.')
    param appServicePlanName string
    
    @description('The name of the App Service plan SKU.')
    param appServicePlanSkuName string
    
    resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = {
      name: appServicePlanName
      location: location
      sku: {
        name: appServicePlanSkuName
      }
    }
    
    resource appServiceApp 'Microsoft.Web/sites@2022-03-01' = {
      name: appServiceAppName
      location: location
      properties: {
        serverFarmId: appServicePlan.id
        httpsOnly: true
      }
    }
    
    @description('The default host name of the App Service app.')
    output appServiceAppHostName string = appServiceApp.properties.defaultHostName
    

    此文件部署 Azure 应用服务计划和应用。 请注意,此模块非常通用。 它不包含有关资源名称或应用服务计划的 SKU 的任何假设。 这使你可以轻松地将模块重复用于不同的部署。

  3. 保存对文件所做的更改。

将模块添加到 Bicep 模板

此处,首先将“应用”模块添加到 Bicep 模板。

  1. 打开 main.bicep 文件。

  2. 将以下参数和变量添加到该文件:

    @description('The Azure region into which the resources should be deployed.')
    param location string = 'westus3'
    
    @description('The name of the App Service app.')
    param appServiceAppName string = 'toy-${uniqueString(resourceGroup().id)}'
    
    @description('The name of the App Service plan SKU.')
    param appServicePlanSkuName string = 'F1'
    
    var appServicePlanName = 'toy-product-launch-plan'
    

    因为这是你打算为玩具网站部署的模板,所以这是一个更具体的模板。 应用服务计划名称定义为一个变量。 SKU 参数具有一个适用于玩具发布网站的默认值。

    提示

    你指定参数 location 应设置为 westus3。 通常,将使用 resourceGroup().location 属性在与资源组相同的位置创建资源。 但是,使用 Microsoft Learn 沙盒时,需要使用与资源组位置不匹配的某些 Azure 区域。

  3. 在参数下,创建一个空行。 现在,键入“应用”模块定义的第一行:

    module app 'modules/app.bicep' = {
    

    键入时,请注意,Visual Studio Code 的 Bicep 扩展可帮助你构建模块声明。 键入模块的路径并键入等号 (=) 字符时,会出现一个弹出菜单,其中有几个选项。

  4. 在弹出菜单中,选择“所需属性”:

    Visual Studio Code 的屏幕截图,其中显示了使用模块所需的属性来构建模块的选项。

  5. 完成模块声明:

    module app 'modules/app.bicep' = {
      name: 'toy-launch-app'
      params: {
        appServiceAppName: appServiceAppName
        appServicePlanName: appServicePlanName
        appServicePlanSkuName: appServicePlanSkuName
        location: location
      }
    }
    
  6. 在文件底部定义输出:

    @description('The host name to use to access the website.')
    output websiteHostName string = app.outputs.appServiceAppHostName
    
  7. 保存对文件所做的更改。

为内容分发网络创建模块

  1. 在“模块”文件夹中,创建一个名为 cdn.bicep 的文件。 保存文件。

  2. 将以下内容添加到 cdn.bicep 文件中:

    @description('The host name (address) of the origin server.')
    param originHostName string
    
    @description('The name of the CDN profile.')
    param profileName string = 'cdn-${uniqueString(resourceGroup().id)}'
    
    @description('The name of the CDN endpoint')
    param endpointName string = 'endpoint-${uniqueString(resourceGroup().id)}'
    
    @description('Indicates whether the CDN endpoint requires HTTPS connections.')
    param httpsOnly bool
    
    var originName = 'my-origin'
    
    resource cdnProfile 'Microsoft.Cdn/profiles@2022-11-01-preview' = {
      name: profileName
      location: 'global'
      sku: {
        name: 'Standard_Microsoft'
      }
    }
    
    resource endpoint 'Microsoft.Cdn/profiles/endpoints@2022-11-01-preview' = {
      parent: cdnProfile
      name: endpointName
      location: 'global'
      properties: {
        originHostHeader: originHostName
        isHttpAllowed: !httpsOnly
        isHttpsAllowed: true
        queryStringCachingBehavior: 'IgnoreQueryString'
        contentTypesToCompress: [
          'text/plain'
          'text/html'
          'text/css'
          'application/x-javascript'
          'text/javascript'
        ]
        isCompressionEnabled: true
        origins: [
          {
            name: originName
            properties: {
              hostName: originHostName
            }
          }
        ]
      }
    }
    
    @description('The host name of the CDN endpoint.')
    output endpointHostName string = endpoint.properties.hostName
    

    此文件部署两个资源:CDN 配置文件和 CDN 终结点。

  3. 保存对文件所做的更改。

将模块添加到主 Bicep 模板

  1. 打开 main.bicep 文件。

  2. appServicePlanSkuName 参数下,添加以下参数:

    @description('Indicates whether a CDN should be deployed.')
    param deployCdn bool = true
    
  3. app 模块定义下,定义 cdn 模块:

    module cdn 'modules/cdn.bicep' = if (deployCdn) {
      name: 'toy-launch-cdn'
      params: {
        httpsOnly: true
        originHostName: app.outputs.appServiceAppHostName
      }
    }
    

    请注意,该模块是有条件的:仅当 deployCdn 参数的值设置为 true 时才会部署该模块。 另请注意,模块的 originHostName 参数会设置为 app 模块的 appServiceAppHostName 输出的值。

  4. 更新主机名称输出,使其选择正确的主机名。 部署 CDN 时,你希望将 CDN 终结点的名称作为主机名。

    output websiteHostName string = deployCdn ? cdn.outputs.endpointHostName : app.outputs.appServiceAppHostName
    
  5. 保存对文件所做的更改。

将 Bicep 模板部署到 Azure

若要将此模板部署到 Azure,你需要从 Visual Studio Code 终端登录到 Azure 帐户。 请确保安装了 Azure CLI,并记得使用你用于激活沙盒的同一帐户登录。

  1. 在“终端”菜单中,选择“新终端”。 终端窗口通常在屏幕的下半部分打开。

  2. 如果终端窗口右侧显示的 shell 为“bash”,则将打开正确的 shell,你可以跳到下一部分。

    Visual Studio Code 终端窗口的屏幕截图,其中显示了 bash 选项。

  3. 如果出现“bash”以外的 shell,请选择 shell 下拉箭头,然后选择“Azure Cloud Shell (Bash)”

    Visual Studio Code 终端窗口的屏幕截图,其中显示了终端 shell 下拉列表并选中了“Git Bash (默认)”。

  4. 在终端 shell 列表中,选择“bash”。

    Visual Studio Code 终端窗口的屏幕截图,其中选中了 bash 终端。

  5. 在终端中,转到保存模板的目录。 例如,如果将模板保存到 templates 文件夹,则可以使用以下命令:

    cd templates
    

安装 Bicep

运行以下命令以确保具有最新版本的 Bicep:

az bicep install && az bicep upgrade

登录 Azure

  1. 在 Visual Studio Code 终端中,运行以下命令登录到 Azure:

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

    Visual Studio Code 终端显示与此帐户关联的订阅列表。

  3. 为在此会话中运行的所有 Azure CLI 命令设置默认订阅。

    az account set --subscription "Concierge Subscription"
    

    注意

    如果最近使用了多个沙盒,终端可能会显示多个 Concierge 订阅实例。 在这种情况下,请使用下面的两个步骤来设置一个默认订阅。 如果前面的命令成功,且仅列出一个 Concierge 订阅,则跳过下面的两个步骤。

  4. 获取 Concierge 订阅 ID。

     az account list \
       --refresh \
       --query "[?contains(name, 'Concierge Subscription')].id" \
       --output table
    
  5. 使用订阅 ID 设置默认订阅。 将 {your subscription ID} 替换为最新的 Concierge 订阅 ID。

    az account set --subscription {your subscription ID}
    

设置默认资源组

当你使用 Azure CLI 时,可以设置默认资源组,并忽略本练习中其他 Azure CLI 命令的参数。 将默认值设置为在沙盒环境中为你创建的资源组。

az configure --defaults group="<rgn>[sandbox resource group name]</rgn>"

将模板部署到 Azure

从 Visual Studio Code 的终端运行以下代码,以将 Bicep 模板部署到 Azure。 此过程可能需要一两分钟才能完成,然后你会看到部署成功。

az deployment group create --template-file main.bicep

终端中将显示状态 Running...

若要将此模板部署到 Azure,需要从 Visual Studio Code 终端登录到 Azure 帐户。 请确保已安装 Azure PowerShell,并且登录的帐户与激活了沙盒的帐户相同。

  1. 在“终端”菜单中,选择“新终端”。 终端窗口通常在屏幕的下半部分打开。

  2. 如果终端窗口右侧显示的 shell 为“powershell”或“pwsh”,则会打开正确的 shell,你可以跳到下一部分。

    Visual Studio Code 终端窗口的屏幕截图,其中在 shell 下拉列表中显示了 pwsh 选项。

  3. 如果出现“powershell”或“pwsh”以外的 shell,请选择 shell 下拉箭头,然后选择“PowerShell”

    Visual Studio Code 终端窗口的屏幕截图,其中显示了终端 shell 下拉列表并选中了 PowerShell。

  4. 在终端 shell 列表中,选择“powershell”或“pwsh”。

    Visual Studio Code 终端窗口的屏幕截图,其中选择了 PowerShell 终端。

  5. 在终端中,转到保存模板的目录。 例如,如果将模板保存到 templates 文件夹,则可以使用以下命令:

    Set-Location -Path templates
    

安装 Bicep CLI

若要从 Azure PowerShell 中使用 Bicep,请安装 Bicep CLI

使用 Azure PowerShell 登录到 Azure

  1. 在 Visual Studio Code 终端中,运行以下命令:

    Connect-AzAccount
    

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

  2. 登录到 Azure 后,终端会显示与此帐户关联的订阅列表。

    如果已激活沙盒,则会显示名为“Concierge 订阅”的订阅。 请在本练习的其余部分使用此订阅。

  3. 为在此会话中运行的所有 Azure PowerShell 命令设置默认订阅。

    $context = Get-AzSubscription -SubscriptionName 'Concierge Subscription'
    Set-AzContext $context
    

    注意

    如果最近使用了多个沙盒,终端可能会显示多个 Concierge 订阅实例。 在这种情况下,请使用下面的两个步骤来设置一个默认订阅。 如果前面的命令成功,且仅列出一个 Concierge 订阅,则跳过下面的两个步骤。

  4. 获取订阅 ID。 运行以下命令将列出你的订阅及其 ID。 查看 Concierge Subscription,然后复制第二列中的 ID。 它类似于 cf49fbbc-217c-4eb6-9eb5-a6a6c68295a0

    Get-AzSubscription
    
  5. 将处于活动状态的订阅更改为“Concierge 订阅”。 请务必将 {Your subscription ID} 替换为复制的内容。

    $context = Get-AzSubscription -SubscriptionId {Your subscription ID}
    Set-AzContext $context
    

设置默认资源组

你可以设置默认资源组,并忽略本练习中其他 Azure PowerShell 命令的参数。 将此默认值设置为在沙盒环境中为你创建的资源组。

Set-AzDefault -ResourceGroupName <rgn>[sandbox resource group name]</rgn>

将模板部署到 Azure

在终端中使用以下 Azure PowerShell 命令将模板部署到 Azure。 这可能需要一两分钟才能完成,然后你会看到部署成功。

New-AzResourceGroupDeployment -TemplateFile main.bicep

查看部署历史记录

  1. 转到 Azure 门户并确保你位于沙盒订阅中:

    1. 选择页面右上角的头像。
    2. 选择“切换目录”。 在列表中,选择“Microsoft Learn 沙盒”目录。
  2. 在左侧面板上,选择“资源组”。

  3. 选择 沙盒资源组名称

  4. 在左侧菜单中,选择“部署”。

    Azure 门户的屏幕截图,其中显示了资源组,并突出显示了“部署”菜单项。

    列出了三个部署。

  5. 选择“main”部署并展开“部署详细信息”。

    请注意,这两个模块都将列出,它们的类型显示为 Microsoft.Resources/deployments。 由于在模板中还会引用这些模块的输出,因此它们将会列出两次。

    Azure 门户的屏幕截图,其中显示了 main 部署的部署详细信息。

  6. 选择“toy-launch-cdn”和“toy-launch-app”部署,然后查看每个部署中部署的资源。 请注意,它们对应于各自模块中定义的资源。

测试网站

  1. 选择“toy-launch-app”部署。

  2. 选择“输出”。

    Azure 门户的屏幕截图,其中显示了部署,并突出显示了“输出”菜单项。

  3. 选择 appServiceAppHostName 输出的复制按钮。

  4. 在新的浏览器标签页上,尝试转到你上一步复制的地址。 地址应该以 https:// 开头。

    Web 应用的欢迎页的屏幕截图,其中的地址栏显示了应用服务主机名。

    此时将显示应用服务欢迎页,其中显示你已成功部署应用。

  5. 转到 main 部署并选择“输出”。

  6. 复制 websiteHostName 输出的值。 请注意,此主机名不一样,因为这是 Azure 内容分发网络主机名。

  7. 在新的浏览器标签页上,尝试转到你上一步复制的主机名。 在地址开头添加 https://

    CDN 终结点需要几分钟才能生效。 如果出现“找不到页面”错误,请等待几分钟,然后再次尝试粘贴链接。 此外,请确保已将 https:// 添加到 URL 的开头,以使用 HTTPS。

    当 CDN 终结点处于活动状态时,将显示相同的应用服务欢迎页。 这一次,它已通过 Azure 内容分发网络服务提供服务,这有助于提高网站性能。

    Web 应用的欢迎页的屏幕截图,其中的地址栏显示了 CDN 终结点。