Share via


クイック スタート: Bicep を使用して予算を作成する

Cost Management での予算は、組織のアカウンタビリティを計画および推進するのに役立ちます。 予算では、特定の期間中に消費またはサブスクライブする Azure サービスを考慮することができます。 コストを事前に管理するために支出を他のユーザーに通知したり、支出の時間変化を監視したりするのに役立ちます。 作成した予算のしきい値を超えた場合は、通知がトリガーされます。 どのリソースも影響を受けることはなく、消費が停止されることはありません。 予算を使用して、コストを分析するときに支出を比較および追跡できます。 このクイックスタートでは、Bicep を使用して 'MyBudget' という名前の予算を作成する方法について説明します。

Bicep は、宣言型の構文を使用して Azure リソースをデプロイするドメイン固有言語 (DSL) です。 簡潔な構文、信頼性の高いタイプ セーフ、およびコードの再利用のサポートが提供されます。 Bicep により、Azure のコード ソリューションとしてのインフラストラクチャに最適な作成エクスペリエンスが実現します。

前提条件

Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。

新しいサブスクリプションをご利用の場合、予算の作成など、Cost Management の機能をすぐには使用できません。 すべての Cost Management 機能を使用できるようになるまでに、最大 48 時間かかる場合があります。

予算は、次の種類の Azure アカウントの種類とスコープに対してサポートされています。

  • Azure ロールベースのアクセス制御 (Azure RBAC) のスコープ
    • 管理グループ
    • サブスクリプション
  • Enterprise Agreement のスコープ
    • 請求先アカウント
    • 部署
    • 登録アカウント
  • 個々の契約
    • 請求先アカウント
  • Microsoft Customer Agreement のスコープ
    • 請求先アカウント
    • 請求プロファイル
    • 請求書セクション
    • Customer
  • AWS スコープ
    • 外部アカウント
    • 外部サブスクリプション

予算を表示するには、少なくとも Azure アカウントの読み取りアクセス権が必要です。

Azure EA サブスクリプションの場合、予算を表示するには読み取りアクセス権が必要です。 予算を作成し、管理するには、共同作成者のアクセス許可が必要です。

ユーザーおよびグループごとの予算については、サブスクリプションに従い、次の Azure アクセス許可 (スコープ) がサポートされています。 スコープの詳細については、「Understand and work with scopes (スコープを理解して使用する)」を参照してください。

  • 所有者: サブスクリプションに対する予算を作成、変更、削除できます。
  • 共同作成者と Cost Management 共同作成者: 自分の予算を作成、変更、削除できます。 他のユーザーによって作成された予算の予算金額を変更できます。
  • 閲覧者と Cost Management 閲覧者 - 自分がアクセス許可を持っている予算を表示できます。

Cost Management データに対するアクセス許可の割り当てについて詳しくは、「Cost Management のデータへのアクセス許可を割り当てる」をご覧ください。

[フィルターなし]

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 ファイルには、1 つの 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> を開始日に置き換えます。 これはその月の 1 日 (YYYY-MM-DD 形式) にする必要があります。 将来の開始日は、3 か月を超えてはいけません。 過去の開始日は、timegrain 期間内で選ぶ必要があります。
    • endDate: <end-date> を終了日 (YYYY-MM-DD 形式) に置き換えます。 指定しない場合、既定値は開始日から 10 年になります。
    • contactEmails: まず、メールを保持する変数を作成してから、その変数を渡します。 サンプルのメール アドレスを、しきい値を超えたときに予算の通知が送信されるメール アドレスに置き換えます。

    Note

    デプロイが完了すると、デプロイが成功したことを示すメッセージが表示されます。

1 つのフィルター

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 ファイルには、1 つの 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> を開始日に置き換えます。 これはその月の 1 日 (YYYY-MM-DD 形式) にする必要があります。 将来の開始日は、3 か月を超えてはいけません。 過去の開始日は、timegrain 期間内で選ぶ必要があります。
    • endDate: <end-date> を終了日 (YYYY-MM-DD 形式) に置き換えます。 指定しない場合、既定値は開始日から 10 年になります。
    • contactEmails: まず、メールを保持する変数を作成してから、その変数を渡します。 サンプルのメール アドレスを、しきい値を超えたときに予算の通知が送信されるメール アドレスに置き換えます。
    • resourceGroupFilterValues: まず、リソース グループのフィルター値を保持する変数を作成し、その変数を渡します。 サンプル フィルターの値を、リソース グループ フィルターの一連の値に置き換えます。

    Note

    デプロイが完了すると、デプロイが成功したことを示すメッセージが表示されます。

2 つ以上のフィルター

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 ファイルには、1 つの 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> を開始日に置き換えます。 これはその月の 1 日 (YYYY-MM-DD 形式) にする必要があります。 将来の開始日は、3 か月を超えてはいけません。 過去の開始日は、timegrain 期間内で選ぶ必要があります。
    • endDate: <end-date> を終了日 (YYYY-MM-DD 形式) に置き換えます。 指定しない場合、既定値は開始日から 10 年になります。
    • contactEmails: まず、メールを保持する変数を作成してから、その変数を渡します。 サンプルのメール アドレスを、しきい値を超えたときに予算の通知が送信されるメール アドレスに置き換えます。
    • contactGroups: まず、連絡先グループを保持する変数を作成してから、その変数を渡します。 サンプルの連絡先グループを、しきい値を超えたときに予算の通知が送信されるアクション グループのリストに置き換えます。 アクション グループのリソース ID を渡す必要があります。このアクション グループは、az monitor action-group show または Get-AzActionGroup で取得できます。
    • resourceGroupFilterValues: まず、リソース グループのフィルター値を保持する変数を作成し、その変数を渡します。 サンプル フィルターの値を、リソース グループ フィルターの一連の値に置き換えます。
    • meterCategoryFilterValues: まず、測定カテゴリ フィルターの値を保持する変数を作成し、その変数を渡します。 かっこ内のサンプル フィルターの値を、測定カテゴリ フィルターの一連の値に置き換えます。

    Note

    デプロイが完了すると、デプロイが成功したことを示すメッセージが表示されます。

デプロイされているリソースを確認する

Azure portal、Azure CLI、または Azure PowerShell を使用して、リソースグループ内のデプロイ済みリソースをリスト表示します。

az consumption budget list

リソースをクリーンアップする

予算が不要になったら、Azure portal、Azure CLI、または Azure PowerShell を使用して削除します。

az consumption budget delete --budget-name MyBudget

次の手順

このクイックスタートでは、Bicep を使用して予算を作成し、デプロイしました。 Cost Management と Billing および Bicep の詳細については、引き続き以下の記事を参照してください。