Bylaw

Gallery › Budget › Azure Community Policy

Deploy subscription budget in MG

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

Deploy a budget for a subscription that will notify the owner once a threshold of 50%, 90%, and 100% of the actual cost of budget is reached. Use the tags 'Owner-Email' and 'Technical-Contact-Email' with email addresses to send emails to those emails.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 254 lines
{
  "name": "6bd66aff-f8f2-4e98-a276-619a78b00e79",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy subscription budget in MG",
    "description": "Deploy a budget for a subscription that will notify the owner once a threshold of 50%, 90%, and 100% of the actual cost of budget is reached. Use the tags 'Owner-Email' and 'Technical-Contact-Email' with email addresses to send emails to those emails.",
    "metadata": {
      "category": "Budget",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "amount": {
        "defaultValue": "1000",
        "metadata": {
          "description": "The total amount of cost or usage to track with the budget",
          "displayName": "amount"
        },
        "type": "String"
      },
      "budgetName": {
        "defaultValue": "budget-set-by-policy",
        "metadata": {
          "description": "The name for the budget to be created",
          "displayName": "budgetName"
        },
        "type": "String"
      },
      "contactRoles": {
        "defaultValue": [
          "Contributor"
        ],
        "metadata": {
          "description": "The list of contact RBAC roles, in an array, to send the budget notification to when the threshold is exceeded.",
          "displayName": "contactRoles"
        },
        "type": "Array"
      },
      "firstThreshold": {
        "defaultValue": "50",
        "metadata": {
          "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 and 1000.",
          "displayName": "firstThreshold"
        },
        "type": "String"
      },
      "secondThreshold": {
        "defaultValue": "90",
        "metadata": {
          "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 and 1000.",
          "displayName": "secondThreshold"
        },
        "type": "String"
      },
      "thirdThreshold": {
        "defaultValue": "100",
        "metadata": {
          "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 and 1000.",
          "displayName": "thirdThreshold"
        },
        "type": "String"
      },
      "timeGrain": {
        "allowedValues": [
          "Monthly",
          "Quarterly",
          "Annually",
          "BillingMonth",
          "BillingQuarter",
          "BillingAnnual"
        ],
        "defaultValue": "BillingMonth",
        "metadata": {
          "description": "The time covered by a budget. Tracking of the amount will be reset based on the time grain.",
          "displayName": "timeGrain"
        },
        "type": "String"
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "Microsoft.Resources/subscriptions",
            "field": "type"
          }
        ]
      },
      "then": {
        "details": {
          "deployment": {
            "location": "westeurope",
            "properties": {
              "mode": "Incremental",
              "parameters": {
                "amount": {
                  "value": "[parameters('amount')]"
                },
                "budgetName": {
                  "value": "[parameters('budgetName')]"
                },
                "contactEmails": {
                  "value": [
                    "[field(concat('tags[','Owner-Email', ']'))]",
                    "[field(concat('tags[','Technical-Contact-Email', ']'))]"
                  ]
                },
                "contactRoles": {
                  "value": "[parameters('contactRoles')]"
                },
                "firstThreshold": {
                  "value": "[parameters('firstThreshold')]"
                },
                "secondThreshold": {
                  "value": "[parameters('secondThreshold')]"
                },
                "thirdThreshold": {
                  "value": "[parameters('thirdThreshold')]"
                },
                "timeGrain": {
                  "value": "[parameters('timeGrain')]"
                }
              },
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "amount": {
                    "type": "String"
                  },
                  "budgetName": {
                    "type": "String"
                  },
                  "contactEmails": {
                    "type": "Array"
                  },
                  "contactRoles": {
                    "type": "Array"
                  },
                  "firstThreshold": {
                    "type": "String"
                  },
                  "secondThreshold": {
                    "type": "String"
                  },
                  "thirdThreshold": {
                    "type": "String"
                  },
                  "startDate": {
                    "defaultValue": "[concat(utcNow('MM'), '/01/', utcNow('yyyy'))]",
                    "type": "String"
                  },
                  "timeGrain": {
                    "type": "String"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2021-10-01",
                    "name": "[parameters('budgetName')]",
                    "properties": {
                      "amount": "[parameters('amount')]",
                      "category": "Cost",
                      "notifications": {
                        "NotificationForExceededBudget1": {
                          "contactEmails": "[parameters('contactEmails')]",
                          "contactRoles": "[parameters('contactRoles')]",
                          "enabled": true,
                          "operator": "GreaterThan",
                          "threshold": "[parameters('firstThreshold')]"
                        },
                        "NotificationForExceededBudget2": {
                          "contactEmails": "[parameters('contactEmails')]",
                          "contactRoles": "[parameters('contactRoles')]",
                          "enabled": true,
                          "operator": "GreaterThan",
                          "threshold": "[parameters('secondThreshold')]"
                        },
                        "NotificationForExceededBudget3": {
                          "contactEmails": "[parameters('contactEmails')]",
                          "contactRoles": "[parameters('contactRoles')]",
                          "enabled": true,
                          "operator": "GreaterThan",
                          "threshold": "[parameters('thirdThreshold')]"
                        },
                        "NotificationForExceededBudget4": {
                          "contactEmails": "[parameters('contactEmails')]",
                          "contactRoles": "[parameters('contactRoles')]",
                          "enabled": true,
                          "operator": "GreaterThan",
                          "threshold": "[parameters('firstThreshold')]",
                          "thresholdType": "Forecasted"
                        },
                        "NotificationForExceededBudget5": {
                          "contactEmails": "[parameters('contactEmails')]",
                          "contactRoles": "[parameters('contactRoles')]",
                          "enabled": true,
                          "operator": "GreaterThan",
                          "threshold": "[parameters('thirdThreshold')]",
                          "thresholdType": "Forecasted"
                        }
                      },
                      "timeGrain": "[parameters('timeGrain')]",
                      "timePeriod": {
                        "startDate": "[parameters('startDate')]"
                      }
                    },
                    "type": "Microsoft.Consumption/budgets"
                  }
                ]
              }
            }
          },
          "deploymentScope": "subscription",
          "existenceCondition": {
            "allOf": [
              {
                "equals": "[parameters('amount')]",
                "field": "Microsoft.Consumption/budgets/amount"
              },
              {
                "equals": "[parameters('timeGrain')]",
                "field": "Microsoft.Consumption/budgets/timeGrain"
              },
              {
                "equals": "Cost",
                "field": "Microsoft.Consumption/budgets/category"
              }
            ]
          },
          "existenceScope": "subscription",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/434105ed-43f6-45c7-a02f-909b2ba83430"
          ],
          "type": "Microsoft.Consumption/budgets"
        },
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw6bd66aff-f8f2-4e98-a276-619a78b00e79.bylaw.ps1 · 80 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '6bd66aff-f8f2-4e98-a276-619a78b00e79' {

    Properties @{
        DisplayName = 'Deploy subscription budget in MG'
        Description = 'Deploy a budget for a subscription that will notify the owner once a threshold of 50%, 90%, and 100% of the actual cost of budget is reached. Use the tags ''Owner-Email'' and ''Technical-Contact-Email'' with email addresses to send emails to those emails.'
        Category    = 'Budget'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter Amount @{
        Default     = '1000'
        DisplayName = 'amount'
        Description = 'The total amount of cost or usage to track with the budget'
    }
    Parameter BudgetName @{
        Default     = 'budget-set-by-policy'
        DisplayName = 'budgetName'
        Description = 'The name for the budget to be created'
    }
    Parameter ContactRoles @{
        Type        = 'Array'
        Default     = @('Contributor')
        DisplayName = 'contactRoles'
        Description = 'The list of contact RBAC roles, in an array, to send the budget notification to when the threshold is exceeded.'
    }
    Parameter FirstThreshold @{
        Default     = '50'
        DisplayName = 'firstThreshold'
        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 and 1000.'
    }
    Parameter SecondThreshold @{
        Default     = '90'
        DisplayName = 'secondThreshold'
        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 and 1000.'
    }
    Parameter ThirdThreshold @{
        Default     = '100'
        DisplayName = 'thirdThreshold'
        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 and 1000.'
    }
    Parameter TimeGrain @{
        AllowedValues = @('Monthly', 'Quarterly', 'Annually', 'BillingMonth', 'BillingQuarter', 'BillingAnnual')
        Default       = 'BillingMonth'
        DisplayName   = 'timeGrain'
        Description   = 'The time covered by a budget. Tracking of the amount will be reset based on the time grain.'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Resources/subscriptions'

    DeployIfNotExists 'Microsoft.Consumption/budgets' -Role 'Cost Management Contributor' -ExistenceScope Subscription -Effect (Param Effect) {
        Require 'amount is the Amount parameter, timeGrain is the TimeGrain parameter and category is Cost' {
            Field amount | Must -Be (Param Amount)
            Field timeGrain | Must -Be (Param TimeGrain)
            Field category | Must -Be 'Cost'
        }
        Deployment @{
            TemplateFile = './templates/6bd66aff-f8f2-4e98-a276-619a78b00e79.json'
            Scope        = 'Subscription'
            Location     = 'westeurope'
            Parameters   = [ordered]@{
                amount          = (Param Amount)
                budgetName      = (Param BudgetName)
                contactEmails   = @('[field(concat(''tags['',''Owner-Email'', '']''))]', '[field(concat(''tags['',''Technical-Contact-Email'', '']''))]')
                contactRoles    = (Param ContactRoles)
                firstThreshold  = (Param FirstThreshold)
                secondThreshold = (Param SecondThreshold)
                thirdThreshold  = (Param ThirdThreshold)
                timeGrain       = (Param TimeGrain)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Budget/deploy-subscription-budget-in-mg/azurepolicy.json in Azure/Community-Policy, under the MIT licence.