练习 - 创建并部署 Azure 资源管理器模板

已完成

注意

首次激活沙盒并接受这些条款后,你的 Microsoft 帐户将与名为“Microsoft Learn 沙盒”的新 Azure 目录相关联。 还会将你添加到名为“Concierge 订阅”的特殊订阅中。

在本练习中,你将创建一个 Azure 资源管理器模板(ARM 模板),将其部署到 Azure,然后更新该 ARM 模板以添加参数和输出。

本练习使用适用于 Visual Studio Code 的 Azure 资源管理器工具。 在开始练习之前,请务必在 Visual Studio Code 中安装此扩展。

创建 ARM 模板

  1. 打开 Visual Studio Code 并创建名为 azuredeploy.json 的新文件

  2. Visual Studio Code ARM 模板扩展拥有配置的代码片段,可帮助你开发模板。 首先,让我们添加一个空白模板。 在文件的第一行上,输入 arm。

  3. Visual Studio Code 可自动显示多个以 arm! 开头的可能选项。 选择“Azure 资源管理器(ARM) 模板”。 Visual Studio Code 会自动处理模板的架构和语言。

    Visual Studio Code azuredeploy.json file showing the snippet choices for Azure Resource Manager templates.

    现在,你的文件如下所示:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {},
      "functions": [],
      "variables": {},
      "resources": [],
      "outputs": {}
    }
    

    请注意,此文件拥有我们在上一单元中讨论过的 ARM 模板的所有部分。

  4. 按 Ctrl+S 保存对文件所做的更改

将 ARM 模板部署到 Azure

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

  1. 选择“终端”>“新建终端”,打开一个终端窗口。

  2. 如果终端窗口的命令栏显示“bash”,即表示你有要使用的正确 shell,可以跳到下一节继续进行操作。

  3. 如果未显示,请选择下拉列表,然后选择“选择默认配置文件”。

    Screenshot of the Visual Studio Code terminal window with bash in the drop-down.

  4. 选择“Git Bash”。

    Screenshot of the Visual Studio Code terminal window showing the select shell drop-down.

  5. 选择“终端”>“新建终端”,打开 bash shell 终端窗口。

登录 Azure

  1. 在终端窗口中,运行此命令以登录到 Azure。

    az login
    
  2. 浏览器将打开,以便你登录到帐户。 登录后,与此帐户关联的订阅列表将显示在终端中。 如果激活了沙盒,应会显示一个名为“Concierge 订阅”的沙盒。 在本练习的其余部分中使用此沙盒。

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

    az account set --subscription "Concierge Subscription"
    

    如果最近使用了多个沙盒,则可能会列出多个“Concierge 订阅”。 如果是这样,请使用接下来的两个步骤来标识和设置默认订阅。

    1. 运行以下命令以获取“Concierge 订阅”ID。
     az account list \
       --refresh \
       --query "[?contains(name, 'Concierge Subscription')].id" \
       --output table
    
    1. 通过运行以下命令并将 {你的订阅 ID} 替换为最新的 Concierge 订阅 ID,来设置默认订阅。
    az account set --subscription {your subscription ID}
    

设置默认资源组

通过将默认资源组设置为激活沙盒时创建的资源组,可以在本练习的 Azure CLI 命令中省略该参数。 若要设置资源组,请运行以下命令。

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

将模板部署到 Azure

运行以下命令将 ARM 模板部署到 Azure。 ARM 模板尚无任何资源,因此你不会看到任何创建的资源。 部署应会成功。

templateFile="azuredeploy.json"
today=$(date +"%d-%b-%Y")
DeploymentName="blanktemplate-"$today

az deployment group create \
 --name $DeploymentName \
 --template-file $templateFile

前面代码的顶部部分设置了 Azure CLI 变量,其中包括要部署的模板文件的路径和部署的名称。 底部部分 az deployment group create将模板部署到 Azure。 请注意,部署名称是 blanktemplate,后缀是一个日期。

终端中应会显示 Running...

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

  1. 在命令栏中,选择“终端”>“新建终端”,打开 PowerShell 窗口。

  2. 如果终端窗口的命令栏显示“PowerShell”,即表示你有要使用的正确 shell,可以跳到下一节继续进行操作。

    Screenshot of the Visual Studio Code terminal window with pwsh terminal selected.

    1. 如果没有显示,请选择向下箭头,然后在下拉列表中选择“PowerShell”。 如果缺少该选项,请选择“选择默认配置文件”。

    2. 在输入字段中,向下滚动并选择“PowerShell”。

      Screenshot of the Visual Studio Code terminal window showing the select shell drop-down.

    3. 选择“终端”>“新建终端”,打开 PowerShell 终端窗口。

使用 Azure PowerShell 登录到 Azure

  1. 从 Visual Studio Code 中的终端运行以下命令以登录到 Azure。 浏览器将打开,你可以登录到帐户。

    Connect-AzAccount
    

    提示

    Az PowerShell 模块是 AzureRM 的替代品,建议使用该版本与 Azure 交互。

  2. 使用激活沙盒时所用的帐户登录。 登录后,Visual Studio Code 将在终端窗口中列出与帐户关联的订阅。 激活沙盒后,将出现一个包含 "name": "Concierge Subscription" 的代码块。 该订阅是其余练习要使用的订阅。

为此会话中的所有 PowerShell 命令设置默认订阅

  1. 运行以下命令以获取订阅及其 ID。 订阅 ID 位于第二列。 查找“Concierge 订阅”,然后复制第二列中的值。 它类似于 cf49fbbc-217c-4eb6-9eb5-a6a6c68295a0

    Get-AzSubscription
    
  2. 运行以下命令,并将 {你的订阅 ID} 替换为在上一步中复制的 ID,以将处于活动状态的订阅更改为 Concierge 订阅。

    $context = Get-AzSubscription -SubscriptionId {Your subscription ID}
    Set-AzContext $context
    
  3. 运行以下命令,以使默认资源组成为在沙盒环境中创建的资源组。 此操作使你能够在本练习的其余 Azure PowerShell 命令中省略该参数。

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

将模板部署到 Azure

通过运行以下命令将模板部署到 Azure。 ARM 模板还没有任何资源,因此你不会看到创建的资源。

$templateFile="azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="blanktemplate-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile

前面代码的顶部设置了 Azure PowerShell 变量,其中包括部署文件的路径和部署的名称。 然后,New-AzResourceGroupDeployment 命令将模板部署到 Azure。 请注意,部署名称是 blanktemplate,后缀是一个日期。

将 ARM 模板部署到 Azure 后,请转到 Azure 门户并确保你在沙盒订阅中。 为此,请选择页面右上角的头像。 选择“切换目录”。 在列表中,选择“Microsoft Learn 沙盒”目录。

  1. 在“资源”菜单中,选择“资源组”。

  2. 选择 [沙盒资源组名称] 资源组

  3. 在“概述”窗格中,你会看到一个成功的部署。

    Azure portal interface for the resource group overview with the deployments section showing that one succeeded.

  4. 选择“1 个已成功”以查看部署的详细信息。

    Azure portal interface for the deployments with the one deployment listed and a succeeded status.

  5. 选择 blanktemplate 以查看部署了哪些资源。 在这种情况下,它将为空,因为尚未在模板中指定任何资源。

    Azure portal interface for the specific deployment with no resources listed.

  6. 使页面在浏览器中处于打开状态。 你将再次检查部署。

向 ARM 模板添加资源

在上一个任务中,你了解了如何创建一个空白模板并部署它。 现在,可以开始部署实际资源了。 在此任务中,你将使用来自适用于 Visual Studio Code 的 Azure 资源管理器工具扩展的代码片段,将 Azure 存储帐户资源添加到 ARM 模板。

  1. 在 Visual Studio Code 中,将光标放在 azuredeploy.json 文件中资源块 "resources":[], 的方括号内。

  2. 在括号内输入“storage”。 此时将显示相关代码段的列表。 选择 arm-storage。

    Visual Studio Code arm-storage snippet shown under the typed word storage.

    你的文件将如下所示:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {},
      "functions": [],
      "variables": {},
      "resources": [
        {
          "name": "storageaccount1",
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2019-06-01",
          "tags": {
            "displayName": "storageaccount1"
          },
          "location": "[resourceGroup().location]",
          "kind": "StorageV2",
          "sku": {
            "name": "Premium_LRS",
            "tier": "Premium"
          }
        }
      ],
      "outputs": {}
    }
    

    你应编辑的值将会突出显示在文件的新部分中,并且可以按 Tab 键来浏览它们。

    请注意,tagslocation 属性已填充了。 location 属性使用函数将资源的位置设置为资源组的位置。 你将在下一个模块中了解标记和函数。

  3. 将资源 name 和 displayName 的值更改为唯一的值(例如 learnexercise12321)。 此名称在整个 Azure 中必须是唯一的,因此请选择对你来说是唯一的名称。

  4. 将 sku 的 name 的值从 Premium_LRS 更改为 Standard_LRS。 将 tier 的值更改为 Standard。 请注意,Visual Studio Code 会通过 IntelliSense 为你的属性值提供正确的选择。 删除默认值(包括引号)并输入引号以查看此工作。

    Screenshot of Visual Studio Code showing the IntelliSense choices for the name attribute of the storage SKU.

  5. 资源位置设置为资源将部署在其中的资源组位置。 保留默认值。

  6. 保存文件。

部署已更新的 ARM 模板

可在此处更改部署的名称以更好地反映此部署的作用。

在终端中运行以下 Azure CLI 命令。 此代码片段与之前使用的代码相同,但部署的名称已更改。

templateFile="azuredeploy.json"
today=$(date +"%d-%b-%Y")
DeploymentName="addstorage-"$today

az deployment group create \
  --name $DeploymentName \
  --template-file $templateFile

在终端中运行以下 Azure PowerShell 命令。 此代码片段与之前使用的代码相同,但部署的名称已更改。

$templateFile="azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addstorage-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile

检查你的部署

  1. 部署完成后,返回到浏览器中的 Azure 门户。 转到资源组,可以看到现在有“2 个已成功”的部署。 选择此链接。

    请注意,此列表中包含了所有两个部署。

    Screenshot of the Azure portal interface for the deployments with the two deployments listed and succeeded statuses.

  2. 选择 addstorage

    Screenshot of the Azure portal interface for the specific deployment with one resource listed.

请注意,存储帐户已部署。