Home Monitor Azure usage with github actions
Post
Cancel

Monitor Azure usage with github actions

This will not work for Azure Plan subscriptions. ((400) Subscription scope usage is not supported for current api version. Please use api version after 2019-10-01)

Need to keep track of your Azure usage? here is a way to use github actions to keep track of your usage. And send you a Teams message if you are over a certain amount.

We will need to enable webhook in Teams and add the webhook to the script, and create a Service Principal with access to the subscription you want to monitor.

Create Service Principal and give it access to the subscription you want to monitor. Copy the output from the command below and add it to the github repository as a secret, AZURE_CREDENTIALS.

1
az ad sp create-for-rbac --name "AzureUsage" --role contributor --scopes /subscriptions/0692777c --sdk-auth

Desktop View

We will need to add secret to the repository, AZURE_CREDENTIALS, and add the output from the command above.

Desktop View

Github actions, it runs every day at midnight.

  • cron: ‘0 0 * * *’ # every day at midnight

This is the workflow file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File: .github/workflows/workflow.yml

on:
  schedule:
   - cron: '0 0 * * *'  # every day at midnight

name: AzureUsage

jobs:

  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    
    - uses: azure/login@v1
      with:
        creds: $
        enable-AzPSSession: true 
        
    - name: Run Azure PowerShell script
      uses: azure/powershell@v1
      with:
        inlinescript: |
          $startDate = (Get-Date).AddDays(-2).ToString("yyyy-MM-dd")
          $endDate = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd")

          $twodaysago = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $startDate | Measure-Object -Property PretaxCost -Sum | Select-Object Sum
          $yesterday = Get-AzConsumptionUsageDetail -StartDate $enddate -EndDate $enddate | Measure-Object -Property PretaxCost -Sum | Select-Object Sum
                    
          $twodaysago = [math]::Floor($twodaysago.sum)
          $yesterday = [math]::ceiling($yesterday.sum)

          $percentof = [math]::Floor($twodaysago * 1.4)

          if ($twodaysago -le $percentof -and $yesterday -lt 100) {
              Write-Host "OK: Yesterday's Azure spending ($yesterday Euro) is not 40% more than 2 days ago ($twodaysago Euro) and not more than 100 Euro | yesterday=$yesterday, spending2daysago=$twodaysago"
          }
          else
          {
          Write-Host  "Alarm!! Tom Much Azure Spending"
          $webhookUri = "webhook from teams"
          $body = @{
          "@context"   = "http://schema.org/extensions"
          "@typecod"      = "MessageCard"
          "themeColor" = "d70000"
          "title"      = "Alarm!! To Much Azure Spending"
          "text"       = "Some thing is wrong in Azure. Yesterday's Azure spending ($yesterday Euro) is 40% more than 2 days ago ($twodaysago Euro) or more than 100 Euro"
          }
          Invoke-RestMethod -Uri $webhookUri -Method Post -Body (ConvertTo-Json -InputObject $body) 
          }
        azPSVersion: "latest"

Desktop View Desktop View

This post is licensed under CC BY 4.0 by the author.