可以通过成本管理中的预算来计划并推动组织责任制。 可以通过预算对特定时期使用或订阅的 Azure 服务进行核算。 可以通过预算将他人的支出通知给本人,方便他们对成本进行前摄性管理,并且可以监视一段时间的支出情况。 超出所创建的预算阈值时,会触发通知。 不会影响资源,也不会停止你对资源的使用。 可以使用预算来比较和跟踪支出,就像分析成本一样。 本快速入门向你展示如何使用 Bicep 创建名为“MyBudget”的预算。
Bicep 是一种特定于域的语言 (DSL),使用声明性语法来部署 Azure 资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep 会针对你的 Azure 基础结构即代码解决方案提供最佳创作体验。
如果没有 Azure 订阅,可在开始前创建一个试用帐户。
如果你有新订阅,则无法立即创建预算或使用其他成本管理功能。 最多可能需要 48 小时才能使用所有成本管理功能。
以下 Azure 帐户类型和范围都支持预算:
- Azure 基于角色的访问控制 (Azure RBAC) 范围
- 管理组
- 订阅
- 企业协议范围
- 计费帐户
- 部门
- 注册帐户
- 个人协议
- 计费帐户
- Microsoft 客户协议范围
- 计费帐户
- 计费配置文件
- 发票科目
- 客户
- AWS 范围
- 外部帐户
- 外部订阅
若要查看预算,你至少需要对 Azure 帐户具有读取访问权限。
对于 Azure EA 订阅,必须拥有读取访问权限才能查看预算。 若要创建和管理预算,必须具有参与者权限。
每个订阅支持以下 Azure 权限或范围,以便按用户和组进行预算。 有关范围的详细信息,请参阅了解并使用范围。
- 所有者:可以为订阅创建、修改或删除预算。
- 参与者和成本管理参与者:可以创建、修改或删除自己的预算。 可以修改其他人创建的预算的预算金额。
- 读取者和成本管理读取者:可以查看他们有权访问的预算。
若要详细了解如何分配对成本管理数据的权限,请参阅分配对成本管理数据的访问权限。
本快速入门中使用的 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@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
}
}
}
}
Bicep 文件中定义了一个 Azure 资源:
将该 Bicep 文件另存为本地计算机上的 main.bicep。
使用 Azure CLI 或 Azure PowerShell 来部署该 Bicep 文件。
myContactEmails ='("user1@contoso.com", "user2@contoso.com")' az deployment sub create --name demoSubDeployment --location chinanorth --template-file main.bicep --parameters startDate=<start-date> endDate=<end-date> contactEmails=$myContactEmails
需要输入以下参数:
- startDate:将 <start-date> 替换为开始日期。 它必须是 YYYY-MM-DD 格式的月份的第一个。 将来的开始日期不能晚于自将来日期算起的三个月。 应在时间范围内选择过去的开始日期。
- endDate:将 <end-date> 替换为 YYYY-MM-DD 格式的结束日期。 如果未提供,则默认为从开始日期开始的 10 年。
- contactEmails:首先创建一个保存电子邮件的变量,然后传递该变量。 将示例电子邮件替换为电子邮件地址,以便在超出阈值时发送预算通知。
备注
部署完成后,应会看到一条指出部署成功的消息。
本快速入门中使用的 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 文件另存为本地计算机上的 main.bicep。
使用 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 chinanorth --template-file main.bicep --parameters startDate=<start-date> endDate=<end-date> contactEmails=$myContactEmails resourceGroupFilterValues=$myRgFilterValues
需要输入以下参数:
- startDate:将 <start-date> 替换为开始日期。 它必须是 YYYY-MM-DD 格式的月份的第一个。 将来的开始日期不能晚于自将来日期算起的三个月。 应在时间范围内选择过去的开始日期。
- endDate:将 <end-date> 替换为 YYYY-MM-DD 格式的结束日期。 如果未提供,则默认为从开始日期开始的 10 年。
- contactEmails:首先创建一个保存电子邮件的变量,然后传递该变量。 将示例电子邮件替换为电子邮件地址,以便在超出阈值时发送预算通知。
- resourceGroupFilterValues 首先创建一个保存资源组筛选器值的变量,然后传递该变量。 将示例筛选器值替换为你的资源组筛选器的值集合。
备注
部署完成后,应会看到一条指出部署成功的消息。
本快速入门中使用的 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 文件另存为本地计算机上的 main.bicep。
使用 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 chinanorth --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 格式的月份的第一个。 将来的开始日期不能晚于自将来日期算起的三个月。 应在时间范围内选择过去的开始日期。
- endDate:将 <end-date> 替换为 YYYY-MM-DD 格式的结束日期。 如果未提供,则默认为从开始日期开始的 10 年。
- contactEmails:首先创建一个保存电子邮件的变量,然后传递该变量。 将示例电子邮件替换为电子邮件地址,以便在超出阈值时发送预算通知。
- contactGroups:首先创建一个保存你的联系人组的变量,然后传递该变量。 将示例联系人组替换为在超过阈值时向其发送预算通知的操作组列表。 必须传递操作组的资源 ID,可以使用 az monitor action-group show 或 Get-AzActionGroup 获取该 ID。
- resourceGroupFilterValues:首先创建一个保存资源组筛选器值的变量,然后传递该变量。 将示例筛选器值替换为你的资源组筛选器的值集合。
- meterCategoryFilterValues:首先创建一个变量来保存你的仪表类别筛选器值,然后传递该变量。 将括号内的示例筛选器值替换为仪表类别筛选器的一组值。
备注
部署完成后,应会看到一条指出部署成功的消息。
使用 Azure 门户、Azure CLI 或 Azure PowerShell 列出资源组中已部署的资源。
az consumption budget list
如果不再需要预算,请使用 Azure 门户、Azure CLI 或 Azure PowerShell 将其删除:
az consumption budget delete --budget-name MyBudget
在本快速入门中,你使用 Bicep 创建和部署了一个预算。 若要详细了解成本管理 + 计费和 Bicep,请继续阅读以下文章。