練習 - 測試和部署已轉換的範本

已完成

您建立了 Bicep 檔案來代表執行玩具卡車服務的虛擬機器。 在確定將該檔案​用於您的管線中之前,您必須先確認該檔案正確代表您目前的 Azure 環境。 在本課程模組中,您將測試 Bicep 檔案,然後部署至現有的 Azure 資源上。

在此過程期間,您將執行下列工作:

  • 執行假設狀況命令以判斷部署的影響。
  • 部署範本並驗證部署狀態。

執行 what-if

在您部署新的 Bicep 檔案之前,請先執行 what-if 命令。 此命令會驗證 Bicep 檔案是否有效。 該命令也會提供部署該檔案時 Azure 環境中將發生的變更清單。

注意

在實際的移轉中,您應該針對生產和非生產環境執行 what-if 命令,並針對每個環境使用適當的參數集。 這樣做可以幫助您偵測在設定中您可能未發現的任何差異。 在此範例中,您會使用單一環境,因此只會針對該環境執行 what-if 作業。

警告

用於 what-if 命令和後續範本部署的部署模式可能帶來風險。 完整模式會刪除資源群組中已定義但範本中未指定的任何資源。 這通常是個好方法,因為其有助於確定 Bicep 程式碼很完整。 但這個選項有些風險,因為您在移轉中可能遺漏資源。

在此練習中,您使用完整模式來執行 what-if 命令,以偵測您可能遺漏的任何資源。 接著,您以增量模式執行部署。

  1. 在 Visual Studio Code 終端中,執行下列命令:

    az deployment group what-if \
      --mode Complete \
      --resource-group ToyTruck \
      --template-file main.bicep \
      --parameters main.parameters.production.json
    
  2. 出現提示時,請輸入安全密碼當作 virtualMachineAdminPassword 參數值。

  1. 在 Visual Studio Code 終端中,執行下列命令:

    New-AzResourceGroupDeployment `
      -WhatIf `
      -Mode Complete `
      -ResourceGroupName ToyTruck `
      -TemplateFile main.bicep `
      -TemplateParameterFile main.parameters.production.json
    
  2. 出現提示時,請輸入安全密碼當作 virtualMachineAdminPassword 參數值。

檢閱 what-if 輸出

檢閱假設狀況輸出,如下範例所示:

Note: The result may contain false positive predictions (noise).
You can help us improve the accuracy of the result by opening an issue here: https://aka.ms/WhatIfIssues.

Resource and property changes are indicated with these symbols:
  - Delete
  ~ Modify
  x NoEffect
  = NoChange

The deployment will update the following scope:

Scope: /subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK

  - Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81

      id:        "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK/providers/Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81"
      location:  "westus3"
      managedBy: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/ToyTruck/providers/Microsoft.Compute/virtualMachines/ToyTruckServer"
      name:      "ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81"
      sku.name:  "Premium_LRS"
      sku.tier:  "Premium"
      type:      "Microsoft.Compute/disks"

  ~ Microsoft.Network/networkInterfaces/toytruckserver123 [2022-05-01]
    - kind:                                              "Regular"
    - properties.allowPort25Out:                         true
    ~ properties.ipConfigurations: [
      ~ 0:

        - properties.privateIPAddress:                        "10.0.0.4"
        - properties.publicIPAddress.properties.deleteOption: "Detach"
        - properties.publicIPAddress.sku:

            name: "Basic"
            tier: "Regional"


      ]
    x properties.ipConfigurations[0].properties.primary: true

  = Microsoft.Compute/virtualMachines/ToyTruckServer [2022-08-01]
    x properties.storageProfile.osDisk.managedDisk.storageAccountType: "Premium_LRS"

  = Microsoft.Network/networkSecurityGroups/ToyTruckServer-nsg [2022-05-01]
  = Microsoft.Network/publicIPAddresses/ToyTruckServer-ip [2022-05-01]
  = Microsoft.Network/virtualNetworks/ToyTruck-vnet [2022-05-01]

Resource changes: 1 to delete, 1 to modify, 4 no change.

輸出包含三件重要的資訊。 讓我們逐一檢閱。

  • what-if 命令偵測到將會刪除受控磁碟。 此輸出不正確。 建立虛擬機器時會自動建立受控磁碟。 雖然受控磁碟會出現在要刪除的資源清單中,虛擬機器會防止刪除它們。 不過,還是謹慎小心,在後續步驟中,您將以增量模式執行實際部署,以降低任何出錯的風險。

  • what-if 命令偵測到 networkInterface 資源的 privateIPAddress 屬性已移除。 此結果沒問題,因為您刻意移除該屬性。 因為 privateIPAllocationMethod 屬性設定為 Dynamic,​所以移除 privateIPAddress 屬性即使是變更也毫無影響。

  • what-if 命令偵測到 networkInterface 資源的 publicIPAddress 的兩個屬性將會被刪除。 您將新增這些屬性來修正問題。

更新範本

main.bicep 中,更新 networkInterface 資源以包含 deleteOptionskupublicIPAddress 屬性:

resource networkInterface 'Microsoft.Network/networkInterfaces@2024-05-01' = {
  name: networkInterfaceName
  location: location
  properties: {
    ipConfigurations: [
      {
        name: 'ipconfig1'
        properties: {
          privateIPAllocationMethod: 'Dynamic'
          publicIPAddress: {
            id: publicIPAddress.id
            properties: {
              deleteOption: 'Detach'
            }
            sku: {
              name: 'Basic'
              tier: 'Regional'
            }
          }
          subnet: {
            id: virtualNetwork::defaultSubnet.id
          }
          primary: true
          privateIPAddressVersion: 'IPv4'
        }
      }
    ]
    enableAcceleratedNetworking: true
    enableIPForwarding: false
    disableTcpStateTracking: false
    networkSecurityGroup: {
      id: networkSecurityGroup.id
    }
    nicType: 'Standard'
  }
}

再次執行 what-if 命令

  1. 在 Visual Studio Code 終端中,執行下列命令:

    az deployment group what-if \
      --mode Complete \
      --resource-group ToyTruck \
      --template-file main.bicep \
      --parameters main.parameters.production.json
    
  2. 出現提示時,請輸入安全密碼當作 virtualMachineAdminPassword 參數值。

  3. 作業完成之後,檢閱輸出。 輸出看起來會像下列的範例。 deleteOptionsku 屬性不會顯示在輸出中。

    Note: The result may contain false positive predictions (noise).
    You can help us improve the accuracy of the result by opening an issue here: https://aka.ms/WhatIfIssues.
    
    Resource and property changes are indicated with these symbols:
      - Delete
      ~ Modify
      x NoEffect
      = NoChange
    
    The deployment will update the following scope:
    
    Scope: /subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK
    
      - Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81
    
          id:        "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK/providers/Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81"
          location:  "westus3"
          managedBy: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/ToyTruck/providers/Microsoft.Compute/virtualMachines/ToyTruckServer"
          name:      "ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81"
          sku.name:  "Premium_LRS"
          sku.tier:  "Premium"
          type:      "Microsoft.Compute/disks"
    
      ~ Microsoft.Network/networkInterfaces/toytruckserver123 [2022-05-01]
        - kind:                                              "Regular"
        - properties.allowPort25Out:                         true
        ~ properties.ipConfigurations: [
          ~ 0:
    
            - properties.privateIPAddress: "10.0.0.4"
    
          ]
        x properties.ipConfigurations[0].properties.primary: true
    
      = Microsoft.Compute/virtualMachines/ToyTruckServer [2022-08-01]
        x properties.storageProfile.osDisk.managedDisk.storageAccountType: "Premium_LRS"
    
      = Microsoft.Network/networkSecurityGroups/ToyTruckServer-nsg [2022-05-01]
      = Microsoft.Network/publicIPAddresses/ToyTruckServer-ip [2022-05-01]
      = Microsoft.Network/virtualNetworks/ToyTruck-vnet [2022-05-01]
    
    Resource changes: 1 to delete, 1 to modify, 4 no change.
    
  1. 在 Visual Studio Code 終端中,執行下列命令:

    New-AzResourceGroupDeployment `
      -WhatIf `
      -Mode Complete `
      -ResourceGroupName ToyTruck `
      -TemplateFile main.bicep `
      -TemplateParameterFile main.parameters.production.json
    
  2. 出現提示時,請輸入安全密碼當作 virtualMachineAdminPassword 參數值。

  3. 作業完成之後,檢閱輸出。 輸出看起來會像下列的範例。 deleteOptionsku 屬性不會顯示在輸出中。

    Note: The result may contain false positive predictions (noise).
    You can help us improve the accuracy of the result by opening an issue here: https://aka.ms/WhatIfIssues.
    
    Resource and property changes are indicated with these symbols:
      - Delete
      ~ Modify
      x NoEffect
      = NoChange
    
    The deployment will update the following scope:
    
    Scope: /subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK
    
      - Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81
    
          id:        "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK/providers/Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81"
          location:  "westus3"
          managedBy: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/ToyTruck/providers/Microsoft.Compute/virtualMachines/ToyTruckServer"
          name:      "ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81"
          sku.name:  "Premium_LRS"
          sku.tier:  "Premium"
          type:      "Microsoft.Compute/disks"
    
      ~ Microsoft.Network/networkInterfaces/toytruckserver123 [2022-05-01]
        - kind:                                              "Regular"
        - properties.allowPort25Out:                         true
        ~ properties.ipConfigurations: [
          ~ 0:
    
            - properties.privateIPAddress: "10.0.0.4"
    
          ]
        x properties.ipConfigurations[0].properties.primary: true
    
      = Microsoft.Compute/virtualMachines/ToyTruckServer [2022-08-01]
        x properties.storageProfile.osDisk.managedDisk.storageAccountType: "Premium_LRS"
    
      = Microsoft.Network/networkSecurityGroups/ToyTruckServer-nsg [2022-05-01]
      = Microsoft.Network/publicIPAddresses/ToyTruckServer-ip [2022-05-01]
      = Microsoft.Network/virtualNetworks/ToyTruck-vnet [2022-05-01]
    
    Resource changes: 1 to delete, 1 to modify, 4 no change.
    

部署範本

您知道您的 Bicep 檔案是有效的,而且 what-if 作業指出您的範本提供了您預期的結果。 您現在可以開始部署範本。 如果此步驟成功,應該不會看到任何影響。

  1. 在 Visual Studio Code 終端中,執行下列命令:

    az deployment group create \
      --resource-group ToyTruck \
      --template-file main.bicep \
      --parameters main.parameters.production.json
    
  2. 出現提示時,請輸入安全密碼當作 virtualMachineAdminPassword 參數值。

    在幾秒鐘內,部署會順利完成。

  3. 在 Azure 入口網站中,開啟資源群組。 選取 [2 個已成功],以檢視部署清單:

    Screenshot of the Azure portal that shows the resource group. The 2 Succeeded link is highlighted.

    部署成功:

    Screenshot of the Azure portal that shows the resource group deployments. The successful deployment is highlighted.

  1. 在 Visual Studio Code 終端中,執行下列命令:

    New-AzResourceGroupDeployment `
      -ResourceGroupName ToyTruck `
      -TemplateFile main.bicep `
      -TemplateParameterFile main.parameters.production.json
    
  2. 出現提示時,請輸入安全密碼當作 virtualMachineAdminPassword 參數值。

    在幾秒鐘內,部署會順利完成。

  3. 在 Azure 入口網站中,開啟資源群組。 選取 [2 個已成功] 連結,以檢視部署清單:

    Screenshot of the Azure portal that shows the resource group. The 2 Succeeded link is highlighted.

    部署成功:

    Screenshot of the Azure portal that shows the resource group deployments. The successful deployment is highlighted.

提示

在實際移轉中,您也應該執行煙霧測試,以驗證資源仍然正確運作。 煙霧測試是最終檢查,以確保您不會進行意外變更。

清除資源

現在您已完成了本練習,您可以移除資源以免因其而產生費用。

在 Visual Studio Code 終端中,執行下列命令:

az group delete --resource-group ToyTruck --yes --no-wait

資源群組和其所有資源會在背景中刪除。

Remove-AzResourceGroup -Name ToyTruck -Force