練習 - 部署延伸模組資源並使用現有的資源

已完成

現在,您已完成為 R&D 小組建立要使用的資料庫,您必須確定已記錄資料庫的存取權。 您有要傳送這些記錄的現有目的地 Log Analytics 工作區。 您也必須將來自研發小組儲存體帳戶的記錄傳送到相同的 Log Analytics 工作區。 在此練習中,您會更新 Bicep 檔案以符合這些需求。

在此過程中,您將會:

  • 建立 Log Analytics 工作區。
  • 更新您的 Bicep 檔案,將診斷設定新增至您的 Cosmos DB 帳戶。
  • 建立儲存體帳戶。
  • 在 Bicep 檔案中,更新適用於儲存體帳戶的診斷設定。
  • 部署您的範本,並驗證結果。

建立 Log Analytics 工作區

建立 Log Analytics 工作區,以模擬已在貴組織中建立的工作區。 使用 Azure CLI 而不是 Bicep。

在終端中執行下列 Azure CLI 命令:

az monitor log-analytics workspace create \
  --workspace-name ToyLogs \
  --location eastus

建立 Log Analytics 工作區,以模擬已在貴組織中建立的工作區。 使用 Azure PowerShell 而不是 Bicep。

在終端中執行下列 Azure PowerShell 命令:

New-AzOperationalInsightsWorkspace `
  -Name ToyLogs `
  -Location eastus

注意

在此範例中,您要將 Log Analytics 工作區部署到與其他資源相同的訂用帳戶和資源群組。 在許多情況下,您會將 Log Analytics 工作區儲存在與您應用程式資源不同的資源群組中。 Bicep 仍可參考這些項目。

新增 Azure Cosmos DB 的診斷設定

您的研發小組必須將所有要求記錄到 Azure Cosmos DB 帳戶。 您決定使用 Azure Cosmos DB 的 Azure 監視器整合來收集 DataPlaneRequests 記錄,其中包含 Azure Cosmos DB 要求的相關資訊。

既然您已建立仿真的現有Log Analytics工作區,您可以從Bicep範本內將其參考為現有資源。 您可以使用其做為 Azure Cosmos DB 記錄的目的地。

若要新增診斷設定,請使用下列步驟:

  1. 在 Visual Studio Code 中,於編輯器中開啟 main.bicep 檔案,然後在現有的變數定義下方新增下列程式碼:

    var logAnalyticsWorkspaceName = 'ToyLogs'
    var cosmosDBAccountDiagnosticSettingsName = 'route-logs-to-log-analytics'
    
  2. 在檔案底部的資源定義下方,新增下列程式碼:

    resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2023-09-01' existing = {
      name: logAnalyticsWorkspaceName
    }
    

    請注意,此資源定義會使用 existing 關鍵字,如果您是透過此 Bicep 範本部署 Log Analytics 工作區,則會刻意省略您通常指定的其他屬性。

  3. 在您剛才新增的程式碼下方,新增下列程式碼:

    resource cosmosDBAccountDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
      scope: cosmosDBAccount
      name: cosmosDBAccountDiagnosticSettingsName
      properties: {
        workspaceId: logAnalyticsWorkspace.id
        logs: [
          {
            category: 'DataPlaneRequests'
            enabled: true
          }
        ]
      }
    }
    

    請注意,此程式碼會部署延伸模組資源。 其會使用 scope 關鍵字來告知 Bicep 資源應該附加至 Azure Cosmos DB 帳戶。 程式碼也會使用 logAnalyticsWorkspace 現有資源的 id 屬性,讓 Azure 知道要將 Azure Cosmos DB 記錄傳送至何處。

建立玩具設計文件的儲存體帳戶

建立 Azure 儲存體帳戶,以模擬您的研發小組已在貴組織中建立了一個。 使用 Azure CLI 而不是 Bicep。

在終端中執行下列 Azure CLI 命令。 將 {storageaccountname} 取代為可能是唯一的名稱。 名稱必須全部小寫、包含 24 個以下的字元,且沒有空格或特殊字元。 記下名稱以供稍後使用。

az storage account create \
  --name {storageaccountname} \
  --location eastus

建立 Azure 儲存體帳戶,以模擬您的研發小組已在貴組織中建立了一個。 使用 Azure PowerShell 而不是 Bicep。

在終端中執行下列 Azure PowerShell 命令。 將 {storageaccountname} 取代為可能是唯一的名稱。 名稱必須全部小寫、包含 24 個以下的字元,且沒有空格或特殊字元。

New-AzStorageAccount `
  -Name {storageaccountname} `
  -Location eastus `
  -SkuName Standard_LRS

新增儲存體帳戶的診斷設定

您的 R&D 小組希望您將所有成功的要求記錄到他們建立的記憶體帳戶。 您決定使用 Azure 儲存體 與 Azure 監視器記錄的整合來達成此目標。 您決定在研發小組的儲存體帳戶上,記錄 blob 儲存體內的所有讀取、寫入和刪除活動。

您必須更新您的 Bicep 範本,以參考您在上一個步驟中建立的儲存體帳戶。

  1. main.bicep 檔案頂端附近的參數定義下方,新增下列參數定義:

    param storageAccountName string
    
  2. 在變數定義下方,新增下列變數定義:

    var storageAccountBlobDiagnosticSettingsName = 'route-logs-to-log-analytics'
    
  3. 在檔案底部的資源定義下,新增下列定義:

    resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
      name: storageAccountName
    
      resource blobService 'blobServices' existing = {
        name: 'default'
      }
    }
    

    請注意,這兩個資源都使用 existing 關鍵字。

  4. 在檔案底部,在您在上一個步驟中新增的記憶體帳戶定義下方,新增下列程序代碼:

    resource storageAccountBlobDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
      scope: storageAccount::blobService
      name: storageAccountBlobDiagnosticSettingsName
      properties: {
        workspaceId: logAnalyticsWorkspace.id
        logs: [
          {
            category: 'StorageRead'
            enabled: true
          }
          {
            category: 'StorageWrite'
            enabled: true
          }
          {
            category: 'StorageDelete'
            enabled: true
          }
        ]
      }
    }
    

    請注意,此延伸模組資源已將其 scope 設定為巢狀結構的現有資源。 Bicep 瞭解其應該將延伸模組資源附加至 blobServices 子資源。

    完成之後,您的完整 Bicep 範本看起來應該像這樣:

    param cosmosDBAccountName string = 'toyrnd-${uniqueString(resourceGroup().id)}'
    param cosmosDBDatabaseThroughput int = 400
    param location string = resourceGroup().location
    param storageAccountName string
    
    var cosmosDBDatabaseName = 'FlightTests'
    var cosmosDBContainerName = 'FlightTests'
    var cosmosDBContainerPartitionKey = '/droneId'
    var logAnalyticsWorkspaceName = 'ToyLogs'
    var cosmosDBAccountDiagnosticSettingsName = 'route-logs-to-log-analytics'
    var storageAccountBlobDiagnosticSettingsName = 'route-logs-to-log-analytics'
    
    resource cosmosDBAccount 'Microsoft.DocumentDB/databaseAccounts@2024-05-15' = {
      name: cosmosDBAccountName
      location: location
      properties: {
        databaseAccountOfferType: 'Standard'
        locations: [
          {
            locationName: location
          }
        ]
      }
    }
    
    resource cosmosDBDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2024-05-15' = {
      parent: cosmosDBAccount
      name: cosmosDBDatabaseName
      properties: {
        resource: {
          id: cosmosDBDatabaseName
        }
        options: {
          throughput: cosmosDBDatabaseThroughput
        }
      }
    
      resource container 'containers' = {
        name: cosmosDBContainerName
        properties: {
          resource: {
            id: cosmosDBContainerName
            partitionKey: {
              kind: 'Hash'
              paths: [
                cosmosDBContainerPartitionKey
              ]
            }
          }
          options: {}
        }
      }
    }
    
    resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2023-09-01' existing = {
      name: logAnalyticsWorkspaceName
    }
    
    resource cosmosDBAccountDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
      scope: cosmosDBAccount
      name: cosmosDBAccountDiagnosticSettingsName
      properties: {
        workspaceId: logAnalyticsWorkspace.id
        logs: [
          {
            category: 'DataPlaneRequests'
            enabled: true
          }
        ]
      }
    }
    
    resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
      name: storageAccountName
    
      resource blobService 'blobServices' existing = {
        name: 'default'
      }
    }
    
    resource storageAccountBlobDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
      scope: storageAccount::blobService
      name: storageAccountBlobDiagnosticSettingsName
      properties: {
        workspaceId: logAnalyticsWorkspace.id
        logs: [
          {
            category: 'StorageRead'
            enabled: true
          }
          {
            category: 'StorageWrite'
            enabled: true
          }
          {
            category: 'StorageDelete'
            enabled: true
          }
        ]
      }
    }
    
  5. 儲存對檔案所做的變更。

將範本部署至 Azure

在終端中執行下列 Azure CLI 命令。 以您在練習中稍早建立的儲存體帳戶名稱取代 {storageaccountname}

az deployment group create \
  --template-file main.bicep \
  --parameters storageAccountName={storageaccountname}

在終端中執行下列 Azure PowerShell 命令。 以您在練習中稍早建立的儲存體帳戶名稱取代 {storageaccountname}

New-AzResourceGroupDeployment `
  -TemplateFile main.bicep `
  -storageAccountName {storageaccountname}

檢查您的部署

  1. 在瀏覽器中,返回 Azure 入口網站。 移至您的資源群組。 您仍然會看到一個成功的部署,因為部署使用與第一個部署相同的名稱。

  2. 選取 [1 個成功] 連結。

  3. 選取名為 main 的部署,然後選取 [部署詳細資料],以展開已部署的資源清單。

    Screenshot of the Azure portal interface for the specific deployment, with the Azure Cosmos DB resources as well as two resources with type Microsoft.Insights/diagnosticSettings.

    請注意,列出的兩個資源是 Microsoft.Insights/diagnosticSettings 類型。 這些資源是您部署的延伸模組資源。 其中一個資源連結至儲存體帳戶,另一個資源連結至 Azure Cosmos DB 帳戶。 現在您可以確認 Azure Cosmos DB 診斷設定已正確設定。

  4. 選取 Azure Cosmos DB 帳戶資源。 入口網站會開啟至 Azure Cosmos DB 帳戶。

    Screenshot of the Azure portal interface for the specific deployment, with the Azure Cosmos DB account highlighted.

  5. 在左上方的 [搜尋] 方塊中,輸入「診斷設定」,然後選取 [診斷設定] 功能表項目。

    Screenshot of the Azure portal interface for the Azure Cosmos DB account, showing the search field with 'Diagnostic settings' entered and the 'Diagnostic settings' menu item highlighted.

  6. Azure 入口網站可能會提示您啟用記錄的全文檢索查詢支援。 這個練習不需要此選項,因此請選取 [現在不要]

    Screenshot of the Azure portal interface for the Azure Cosmos DB account showing Diagnostic settings page with prompt to enable full-text query.

  7. 請注意,有一 個名為 route-logs-to-log-analytics 的診斷設定,其設定為將記錄路由傳送至 ToyLogs 工作區。

    Screenshot of the Azure portal interface for the Azure Cosmos DB account, showing the diagnostic settings.

    想要的話,您也可以驗證儲存體帳戶是否對 blob 儲存體啟用類似的診斷設定。