Share via


快速入門:使用 Bicep 建立預算

成本管理中的預算可協助您進行規劃並促進組織責任歸屬。 透過預算,您可以說明您在特定期間所取用或訂閱的 Azure 服務。 這些服務可協助您通知其他人有關費用的資訊,以主動管理成本,並監視長期的費用變化。 超過您所建立的預算閾值時,就會觸發通知。 您的資源都會不受到影響,而您的使用量並不會停止。 當您分析成本時,您可以使用預算來比較及追蹤費用。 本快速入門會示範如何使用 Bicep 範本建立命名為「MyBudget」的預算。

Bicep 是使用宣告式語法來部署 Azure 資源的特定領域語言 (DSL)。 其提供簡潔的語法、可靠的類型安全,並支援程式碼重複使用。 Bicep 能夠為您在 Azure 中的基礎結構即程式碼解決方案,提供最佳的製作體驗。

必要條件

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

如果您有新的訂用帳戶,就無法立即建立預算或使用成本管理功能。 最多可能需要48小時的時間,才能使用所有的成本管理功能。

預算受下列類型的 Azure 帳戶類型和範圍支援:

  • Azure 角色型存取控制 (Azure RBAC) 範圍
    • 管理群組
    • 訂用帳戶
  • Enterprise 合約範圍
    • 計費帳戶
    • department
    • 註冊帳戶
  • 個別合約
    • 計費帳戶
  • Microsoft 客戶合約範圍
    • 計費帳戶
    • 帳單設定檔
    • 發票區段
    • 客戶
  • AWS 範圍
    • 外部帳戶
    • 外部訂用帳戶

若要檢視預算,您至少需要 Azure 帳戶的讀取存取。

針對 Azure EA 訂用帳戶,您必須具備檢視預算的讀取存取權。 若要建立及管理預算,您必須具有參與者權限。

使用者和群組針對預算的每個訂用帳戶支援下列 Azure 權限或範圍。 如需有關範圍的詳細資訊,請參閱了解並使用範圍

  • 擁有者: 可以建立、修改或刪除訂用帳戶的預算。
  • 參與者和成本管理參與者: 可以建立、修改或刪除自己的預算。 可以修改其他人所建立之預算的預算金額。
  • 讀取者和成本管理讀取者: 可以檢視他們有權限的預算。

如需成本管理資料的指派權限詳細資訊,請參閱指派成本管理資料的存取權

沒有篩選

檢閱 Bicep 檔案

此快速入門中使用的 Bicep 檔案是來自 Azure 快速入門範本

targetScope = 'subscription'

@description('Name of the Budget. It should be unique within a resource group.')
param budgetName string = 'MyBudget'

@description('The total amount of cost or usage to track with the budget')
param amount int = 1000

@description('The time covered by a budget. Tracking of the amount will be reset based on the time grain.')
@allowed([
  'Monthly'
  'Quarterly'
  'Annually'
])
param timeGrain string = 'Monthly'

@description('The start date must be first of the month in YYYY-MM-DD format. Future start date should not be more than three months. Past start date should be selected within the timegrain preiod.')
param startDate string

@description('The end date for the budget in YYYY-MM-DD format. If not provided, we default this to 10 years from the start date.')
param endDate string

@description('Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0.01 and 1000.')
param firstThreshold int = 90

@description('Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0.01 and 1000.')
param secondThreshold int = 110

@description('The list of email addresses to send the budget notification to when the threshold is exceeded.')
param contactEmails array

resource budget 'Microsoft.Consumption/budgets@2023-11-01' = {
  name: budgetName
  properties: {
    timePeriod: {
      startDate: startDate
      endDate: endDate
    }
    timeGrain: timeGrain
    amount: amount
    category: 'Cost'
    notifications: {
      NotificationForExceededBudget1: {
        enabled: true
        operator: 'GreaterThan'
        threshold: firstThreshold
        contactEmails: contactEmails
      }
      NotificationForExceededBudget2: {
        enabled: true
        operator: 'GreaterThan'
        threshold: secondThreshold
        contactEmails: contactEmails
      }
    }
  }
}

output name string = budget.name
output resourceId string = budget.id

Bicep 檔案中定義了一項 Azure 資源:

部署 Bicep 檔案

  1. 將 Bicep 檔案以 main.bicep 儲存至本機電腦。

  2. 使用 Azure CLI 或 Azure PowerShell 部署 Bicep 檔案。

    myContactEmails ='("user1@contoso.com", "user2@contoso.com")'
    
    az deployment sub create --name demoSubDeployment --location centralus --template-file main.bicep --parameters startDate=<start-date> endDate=<end-date> contactEmails=$myContactEmails
    

    您必須輸入下列參數:

    • startDate:以開始日期取代 <start-date>。 必須以 YYYY-MM-DD 格式填入當月的第一天。 未來開始日期不應超過接下來的三個月。 過去的開始日期應於 timegrain 範圍內選取。
    • endDate:以 YYYY-MM-DD 格式的結束日期取代 <end-date>。 若未輸入,則預設為從開始日期起算的十年。
    • contactEmails:先建立保存電子郵件的變數,然後傳遞變數。 以電子郵件地址來取代範例電子郵件,即可在超過閾值時傳送預算通知至填入的電子郵件。

    注意

    當部署完成時,您應該會看到指出部署成功的訊息。

一個篩選條件

檢閱 Bicep 檔案

此快速入門中使用的 Bicep 檔案是來自 Azure 快速入門範本

targetScope = 'subscription'

@description('Name of the Budget. It should be unique within a resource group.')
param budgetName string = 'MyBudget'

@description('The total amount of cost or usage to track with the budget')
param amount int = 1000

@description('The time covered by a budget. Tracking of the amount will be reset based on the time grain.')
@allowed([
  'Monthly'
  'Quarterly'
  'Annually'
])
param timeGrain string = 'Monthly'

@description('The start date must be first of the month in YYYY-MM-DD format. Future start date should not be more than three months. Past start date should be selected within the timegrain preiod.')
param startDate string

@description('The end date for the budget in YYYY-MM-DD format. If not provided, we default this to 10 years from the start date.')
param endDate string

@description('Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0.01 and 1000.')
param firstThreshold int = 90

@description('Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0.01 and 1000.')
param secondThreshold int = 110

@description('The list of email addresses to send the budget notification to when the threshold is exceeded.')
param contactEmails array

@description('The set of values for the resource group filter.')
param resourceGroupFilterValues array

resource budget 'Microsoft.Consumption/budgets@2021-10-01' = {
  name: budgetName
  properties: {
    timePeriod: {
      startDate: startDate
      endDate: endDate
    }
    timeGrain: timeGrain
    amount: amount
    category: 'Cost'
    notifications: {
      NotificationForExceededBudget1: {
        enabled: true
        operator: 'GreaterThan'
        threshold: firstThreshold
        contactEmails: contactEmails
      }
      NotificationForExceededBudget2: {
        enabled: true
        operator: 'GreaterThan'
        threshold: secondThreshold
        contactEmails: contactEmails
      }
    }
    filter: {
      dimensions: {
        name: 'ResourceGroupName'
        operator: 'In'
        values: resourceGroupFilterValues
      }
    }
  }
}

Bicep 檔案中定義了一項 Azure 資源:

部署 Bicep 檔案

  1. 將 Bicep 檔案以 main.bicep 儲存至本機電腦。

  2. 使用 Azure CLI 或 Azure PowerShell 部署 Bicep 檔案。

    myContactEmails ='("user1@contoso.com", "user2@contoso.com")'
    myRgFilterValues ='("resource-group-01", "resource-group-02")'
    
    az deployment sub create --name demoSubDeployment --location centralus --template-file main.bicep --parameters startDate=<start-date> endDate=<end-date> contactEmails=$myContactEmails resourceGroupFilterValues=$myRgFilterValues
    

    您必須輸入下列參數:

    • startDate:以開始日期取代 <start-date>。 必須以 YYYY-MM-DD 格式填入當月的第一天。 未來開始日期不應超過接下來的三個月。 過去的開始日期應於 timegrain 範圍內選取。
    • endDate:以 YYYY-MM-DD 格式的結束日期取代 <end-date>。 若未輸入,則預設為從開始日期起算的十年。
    • contactEmails:先建立保存電子郵件的變數,然後傳遞變數。 以電子郵件地址來取代範例電子郵件,即可在超過閾值時傳送預算通知至填入的電子郵件。
    • resourceGroupFilterValues:先建立保存資源群組篩選值的變數,然後傳遞變數。 以資源群組篩選的值集來取代範例篩選值。

    注意

    當部署完成時,您應該會看到指出部署成功的訊息。

兩個或多個篩選條件

檢閱 Bicep 檔案

此快速入門中使用的 Bicep 檔案是來自 Azure 快速入門範本

targetScope = 'subscription'

@description('Name of the Budget. It should be unique within a resource group.')
param budgetName string = 'MyBudget'

@description('The total amount of cost or usage to track with the budget')
param amount int = 1000

@description('The time covered by a budget. Tracking of the amount will be reset based on the time grain.')
@allowed([
  'Monthly'
  'Quarterly'
  'Annually'
])
param timeGrain string = 'Monthly'

@description('The start date must be first of the month in YYYY-MM-DD format. Future start date should not be more than three months. Past start date should be selected within the timegrain preiod.')
param startDate string

@description('The end date for the budget in YYYY-MM-DD format. If not provided, we default this to 10 years from the start date.')
param endDate string

@description('Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0.01 and 1000.')
param firstThreshold int = 90

@description('Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0.01 and 1000.')
param secondThreshold int = 110

@description('The list of contact roles to send the budget notification to when the threshold is exceeded.')
param contactRoles array = [
  'Owner'
  'Contributor'
  'Reader'
]

@description('The list of email addresses to send the budget notification to when the threshold is exceeded.')
param contactEmails array

@description('The list of action groups to send the budget notification to when the threshold is exceeded. It accepts array of strings.')
param contactGroups array

@description('The set of values for the resource group filter.')
param resourceGroupFilterValues array

@description('The set of values for the meter category filter.')
param meterCategoryFilterValues array

resource budget 'Microsoft.Consumption/budgets@2021-10-01' = {
  name: budgetName
  properties: {
    timePeriod: {
      startDate: startDate
      endDate: endDate
    }
    timeGrain: timeGrain
    amount: amount
    category: 'Cost'
    notifications: {
      NotificationForExceededBudget1: {
        enabled: true
        operator: 'GreaterThan'
        threshold: firstThreshold
        contactEmails: contactEmails
        contactRoles: contactRoles
        contactGroups: contactGroups
      }
      NotificationForExceededBudget2: {
        enabled: true
        operator: 'GreaterThan'
        threshold: secondThreshold
        contactEmails: contactEmails
        contactRoles: contactRoles
        contactGroups: contactGroups
        thresholdType: 'Forecasted'
      }
    }
    filter: {
      and: [
        {
          dimensions: {
            name: 'ResourceGroupName'
            operator: 'In'
            values: resourceGroupFilterValues
          }
        }
        {
          dimensions: {
            name: 'MeterCategory'
            operator: 'In'
            values: meterCategoryFilterValues
          }
        }
      ]
    }
  }
}

Bicep 檔案中定義了一項 Azure 資源:

部署 Bicep 檔案

  1. 將 Bicep 檔案以 main.bicep 儲存至本機電腦。

  2. 使用 Azure CLI 或 Azure PowerShell 部署 Bicep 檔案。

    myContactEmails ='("user1@contoso.com", "user2@contoso.com")'
    myContactGroups ='("/subscriptions/{sub-id}/resourceGroups/{rg-name}/providers/microsoft.insights/actionGroups/groupone", "/subscriptions/{sub-id}/resourceGroups/{rg-name}/providers/microsoft.insights/actionGroups/grouptwo")'
    myRgFilterValues ='("resource-group-01", "resource-group-02")'
    myMeterCategoryFilterValues ='("meter-category-01", "meter-category-02")'
    
    az deployment sub create --name demoSubDeployment --location centralus --template-file main.bicep --parameters startDate=<start-date> endDate=<end-date> contactEmails=$myContactEmails contactGroups=$myContactGroups resourceGroupFilterValues=$myRgFilterValues meterCategoryFilterValues=$myMeterCategoryFilterValues
    

    您必須輸入下列參數:

    • startDate:以開始日期取代 <start-date>。 必須以 YYYY-MM-DD 格式填入當月的第一天。 未來開始日期不應超過接下來的三個月。 過去的開始日期應於 timegrain 範圍內選取。
    • endDate:以 YYYY-MM-DD 格式的結束日期取代 <end-date>。 若未輸入,則預設為從開始日期起算的十年。
    • contactEmails:先建立保存電子郵件的變數,然後傳遞變數。 以電子郵件地址來取代範例電子郵件,即可在超過閾值時傳送預算通知至填入的電子郵件。
    • contactGroups:先建立保存連絡人群組的變數,然後傳遞變數。 以動作群組清單來取代範例連絡人群組,即可在超過閾值時傳送預算通知給動作群組。 您必須傳遞動作群組的資源識別碼,您可以使用 az monitor action-group showGet-AzActionGroup 來取得。
    • resourceGroupFilterValues:先建立保存資源群組篩選值的變數,然後傳遞變數。 以資源群組篩選的值集來取代範例篩選值。
    • resourceGroupFilterValues:先建立保存計量類別篩選值的變數,然後傳遞變數。 以計量類別篩選的值集來取代括弧內的範例篩選值。

    注意

    當部署完成時,您應該會看到指出部署成功的訊息。

檢閱已部署的資源

使用 Azure 入口網站、Azure CLI 或 Azure PowerShell 來列出資源群組中已部署的資源。

az consumption budget list

清除資源

您不再需要該預算時,請使用 Azure 入口網站、Azure CLI 或 Azure PowerShell 來刪除:

az consumption budget delete --budget-name MyBudget

下一步

在本快速入門中,您使用了 Bicep 來建立預算並加以部署。 如需深入瞭解「成本管理與計費」及 Bicep,請繼續閱讀下列文章。