Mulai Cepat: Membuat anggaran dengan Bicep

Anggaran dalam Cost Management membantu Anda merencanakan dan mendorong akuntabilitas organisasi. Dengan anggaran, Anda dapat memperhitungkan layanan Azure yang Anda konsumsi atau Anda langgani selama periode tertentu. Hal ini membantu Anda memberi tahu orang lain tentang pengeluarannya untuk mengelola biaya secara proaktif dan memantau bagaimana pengeluaran berlangsung dari waktu ke waktu. Saat ambang anggaran yang Anda buat terlampaui, pemberitahuan akan dipicu. Tidak ada sumber daya Anda yang terpengaruh dan penggunaan Anda tidak dihentikan. Anda dapat menggunakan anggaran untuk membandingkan dan melacak pengeluaran saat menganalisis biaya. Mulai cepat ini menunjukkan kepada Anda cara membuat anggaran bernama 'MyBudget' menggunakan Bicep.

Bicep adalah bahasa pemrogram khusus domain (DSL) yang menggunakan sintaks deklaratif untuk menyebarkan sumber daya Azure. Bicep menyediakan sintaks ringkas, keamanan jenis yang andal, dan dukungan untuk penggunaan kembali kode. Bicep menawarkan pengalaman penulisan terbaik untuk solusi infrastructure-as-code di Azure.

Prasyarat

Jika Anda tidak memiliki langganan Azure, buat akun gratis sebelum Anda memulai.

Jika Anda memiliki langganan baru, Anda tidak dapat langsung membuat anggaran atau menggunakan fitur Cost Management lainnya. Mungkin diperlukan waktu hingga 48 jam sampai Anda dapat menggunakan semua fitur Cost Management.

Anggaran didukung untuk jenis cakupan dan jenis akun Azure berikut:

  • Cakupan kontrol akses berbasis peran Azure (Azure RBAC)
    • Grup pengelolaan
    • Langganan
  • Cakupan Perjanjian Enterprise
    • Akun tagihan
    • Departemen
    • Akun pendaftaran
  • Perjanjian individu
    • Akun tagihan
  • Cakupan Perjanjian Pelanggan Microsoft
    • Akun tagihan
    • Profil tagihan
    • Bagian faktur
    • Pelanggan
  • Cakupan AWS
    • Akun eksternal
    • Langganan eksternal

Untuk melihat anggaran, Anda memerlukan setidaknya akses baca untuk akun Azure Anda.

Untuk langganan Azure EA, Anda harus memiliki akses baca untuk melihat anggaran. Untuk membuat dan mengelola anggaran, Anda harus memiliki izin kontributor.

Izin atau cakupan Azure berikut, didukung per langganan untuk anggaran menurut pengguna dan grup. Untuk mengetahui informasi selengkapnya tentang cakupan, lihat Memahami dan menggunakan cakupan.

  • Pemilik: Dapat membuat, memodifikasi, atau menghapus anggaran untuk langganan.
  • Kontributor dan kontributor Cost Management: Dapat membuat, memodifikasi, atau menghapus anggaran mereka sendiri. Dapat memodifikasi jumlah anggaran untuk anggaran yang dibuat oleh orang lain.
  • Pembaca dan pembaca Cost Management: Dapat melihat anggaran yang izinnya dimiliki.

Untuk mengetahui informasi selengkapnya tentang menetapkan izin ke data Cost Management, lihat Menetapkan akses ke data Cost Management.

Tanpa filter

Tinjau file Bicep

File Bicep yang digunakan dalam mulai cepat berasal dari Templat Mulai Cepat 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

Satu sumber daya Azure ditentukan dalam file Bicep:

Menerapkan file Bicep

  1. Simpan file Bicep sebagai main.bicep ke komputer lokal Anda.

  2. Sebarkan file Bicep menggunakan Azure CLI atau Azure PowerShell.

    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
    

    Anda harus memasukkan parameter berikut:

    • startDate: Ganti <tanggal mulai> dengan tanggal mulai. Ini harus menjadi yang pertama dari bulan dalam format YYYY-MM-DD. Tanggal mulai di masa depan tidak boleh lebih dari tiga bulan di masa depan. Tanggal mulai sebelumnya harus dipilih dalam periode timegrain.
    • endDate: Ganti <tanggal akhir> dengan tanggal akhir dalam format YYYY-MM-DD. Jika tidak disediakan, defaultnya adalah sepuluh tahun dari tanggal mulai.
    • contactEmails: Pertama-tama buat variabel yang menyimpan email Anda, lalu teruskan variabel tersebut. Ganti contoh email dengan alamat email untuk mengirim pemberitahuan anggaran saat ambang batas terlampaui.

    Catatan

    Setelah penyebaran selesai, Anda akan melihat pesan yang menunjukkan penyebaran berhasil.

Satu filter

Tinjau file Bicep

File Bicep yang digunakan dalam mulai cepat berasal dari Templat Mulai Cepat 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
      }
    }
  }
}

Satu sumber daya Azure ditentukan dalam file Bicep:

Menerapkan file Bicep

  1. Simpan file Bicep sebagai main.bicep ke komputer lokal Anda.

  2. Sebarkan file Bicep menggunakan Azure CLI atau Azure PowerShell.

    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
    

    Anda harus memasukkan parameter berikut:

    • startDate: Ganti <tanggal mulai> dengan tanggal mulai. Ini harus menjadi yang pertama dari bulan dalam format YYYY-MM-DD. Tanggal mulai di masa depan tidak boleh lebih dari tiga bulan di masa depan. Tanggal mulai sebelumnya harus dipilih dalam periode timegrain.
    • endDate: Ganti <tanggal akhir> dengan tanggal akhir dalam format YYYY-MM-DD. Jika tidak disediakan, defaultnya adalah sepuluh tahun dari tanggal mulai.
    • contactEmails: Pertama-tama buat variabel yang menyimpan email Anda, lalu teruskan variabel tersebut. Ganti contoh email dengan alamat email untuk mengirim pemberitahuan anggaran saat ambang batas terlampaui.
    • resourceGroupFilterValues Pertama-tama buat variabel yang menyimpan nilai filter grup sumber daya Anda, lalu teruskan variabel tersebut. Ganti nilai filter sampel dengan set nilai untuk filter grup sumber daya Anda.

    Catatan

    Setelah penyebaran selesai, Anda akan melihat pesan yang menunjukkan penyebaran berhasil.

Dua atau lebih filter

Tinjau file Bicep

File Bicep yang digunakan dalam mulai cepat berasal dari Templat Mulai Cepat 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
          }
        }
      ]
    }
  }
}

Satu sumber daya Azure ditentukan dalam file Bicep:

Menerapkan file Bicep

  1. Simpan file Bicep sebagai main.bicep ke komputer lokal Anda.

  2. Sebarkan file Bicep menggunakan Azure CLI atau Azure PowerShell.

    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
    

    Anda harus memasukkan parameter berikut:

    • startDate: Ganti <tanggal mulai> dengan tanggal mulai. Ini harus menjadi yang pertama dari bulan dalam format YYYY-MM-DD. Tanggal mulai di masa depan tidak boleh lebih dari tiga bulan di masa depan. Tanggal mulai sebelumnya harus dipilih dalam periode timegrain.
    • endDate: Ganti <tanggal akhir> dengan tanggal akhir dalam format YYYY-MM-DD. Jika tidak disediakan, defaultnya adalah sepuluh tahun dari tanggal mulai.
    • contactEmails: Pertama-tama buat variabel yang menyimpan email Anda, lalu teruskan variabel tersebut. Ganti contoh email dengan alamat email untuk mengirim pemberitahuan anggaran saat ambang batas terlampaui.
    • contactGroups: Pertama-tama buat variabel yang menyimpan grup kontak Anda, lalu teruskan variabel tersebut. Ganti sampel grup kontak dengan daftar grup tindakan untuk mengirim pemberitahuan anggaran saat ambang batas terlampaui. Anda harus meneruskan ID sumber daya grup tindakan, yang bisa Anda dapatkan dengan az monitor action-group show atau Get-AzActionGroup.
    • resourceGroupFilterValues: Pertama-tama buat variabel yang menyimpan nilai filter grup sumber daya Anda, lalu teruskan variabel tersebut. Ganti nilai filter sampel dengan set nilai untuk filter grup sumber daya Anda.
    • meterCategoryFilterValues: Pertama-tama buat variabel yang menyimpan nilai filter kategori meter Anda, lalu teruskan variabel tersebut. Ganti nilai filter sampel dalam tanda kurung dengan set nilai untuk filter kategori meter Anda.

    Catatan

    Setelah penyebaran selesai, Anda akan melihat pesan yang menunjukkan penyebaran berhasil.

Meninjau sumber daya yang disebarkan

Gunakan portal Microsoft Azure, Azure CLI, atau Azure PowerShell untuk mencantumkan sumber daya yang disebarkan dalam grup sumber daya.

az consumption budget list

Membersihkan sumber daya

Saat anggaran tidak lagi diperlukan, gunakan portal Azure, Azure CLI, atau Azure PowerShell untuk menghapusnya:

az consumption budget delete --budget-name MyBudget

Langkah berikutnya

Dalam mulai cepat ini, Anda membuat anggaran dan menyebarkannya menggunakan Bicep. Untuk mempelajari lebih lanjut tentang Cost Management, Billing, dan Bicep, lanjutkan ke artikel di bawah ini.