Bylaw

Gallery › Budget › Azure Landing Zones Library

Deploy a default budget on all subscriptions under the assigned scope

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.1.0

Deploy a default budget on all subscriptions under the assigned scope

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

Azure Policy JSONDeploy-Budget.alz_policy_definition.json · 236 lines
{
  "name": "Deploy-Budget",
  "properties": {
    "description": "Deploy a default budget on all subscriptions under the assigned scope",
    "displayName": "Deploy a default budget on all subscriptions under the assigned scope",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureUSGovernment"
      ],
      "category": "Budget",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.1.0"
    },
    "mode": "All",
    "parameters": {
      "amount": {
        "defaultValue": "1000",
        "metadata": {
          "description": "The total amount of cost or usage to track with the budget"
        },
        "type": "String"
      },
      "budgetName": {
        "defaultValue": "budget-set-by-policy",
        "metadata": {
          "description": "The name for the budget to be created"
        },
        "type": "String"
      },
      "contactEmails": {
        "defaultValue": [],
        "metadata": {
          "description": "The list of email addresses, in an array, to send the budget notification to when the threshold is exceeded."
        },
        "type": "Array"
      },
      "contactGroups": {
        "defaultValue": [],
        "metadata": {
          "description": "The list of action groups, in an array, to send the budget notification to when the threshold is exceeded. It accepts array of strings."
        },
        "type": "Array"
      },
      "contactRoles": {
        "defaultValue": [
          "Owner",
          "Contributor"
        ],
        "metadata": {
          "description": "The list of contact RBAC roles, in an array, to send the budget notification to when the threshold is exceeded."
        },
        "type": "Array"
      },
      "effect": {
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists",
        "metadata": {
          "description": "Enable or disable the execution of the policy"
        },
        "type": "String"
      },
      "firstThreshold": {
        "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."
        },
        "type": "String"
      },
      "secondThreshold": {
        "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."
        },
        "type": "String"
      },
      "timeGrain": {
        "allowedValues": [
          "Monthly",
          "Quarterly",
          "Annually",
          "BillingMonth",
          "BillingQuarter",
          "BillingAnnual"
        ],
        "defaultValue": "Monthly",
        "metadata": {
          "description": "The time covered by a budget. Tracking of the amount will be reset based on the time grain."
        },
        "type": "String"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "Microsoft.Resources/subscriptions",
            "field": "type"
          }
        ]
      },
      "then": {
        "details": {
          "deployment": {
            "location": "northeurope",
            "properties": {
              "mode": "Incremental",
              "parameters": {
                "amount": {
                  "value": "[parameters('amount')]"
                },
                "budgetName": {
                  "value": "[parameters('budgetName')]"
                },
                "contactEmails": {
                  "value": "[parameters('contactEmails')]"
                },
                "contactGroups": {
                  "value": "[parameters('contactGroups')]"
                },
                "contactRoles": {
                  "value": "[parameters('contactRoles')]"
                },
                "firstThreshold": {
                  "value": "[parameters('firstThreshold')]"
                },
                "secondThreshold": {
                  "value": "[parameters('secondThreshold')]"
                },
                "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"
                  },
                  "contactGroups": {
                    "type": "Array"
                  },
                  "contactRoles": {
                    "type": "Array"
                  },
                  "firstThreshold": {
                    "type": "String"
                  },
                  "secondThreshold": {
                    "type": "String"
                  },
                  "startDate": {
                    "defaultValue": "[concat(utcNow('MM'), '/01/', utcNow('yyyy'))]",
                    "type": "String"
                  },
                  "timeGrain": {
                    "type": "String"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2019-10-01",
                    "name": "[parameters('budgetName')]",
                    "properties": {
                      "amount": "[parameters('amount')]",
                      "category": "Cost",
                      "notifications": {
                        "NotificationForExceededBudget1": {
                          "contactEmails": "[parameters('contactEmails')]",
                          "contactGroups": "[parameters('contactGroups')]",
                          "contactRoles": "[parameters('contactRoles')]",
                          "enabled": true,
                          "operator": "GreaterThan",
                          "threshold": "[parameters('firstThreshold')]"
                        },
                        "NotificationForExceededBudget2": {
                          "contactEmails": "[parameters('contactEmails')]",
                          "contactGroups": "[parameters('contactGroups')]",
                          "contactRoles": "[parameters('contactRoles')]",
                          "enabled": true,
                          "operator": "GreaterThan",
                          "threshold": "[parameters('secondThreshold')]"
                        }
                      },
                      "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/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "type": "Microsoft.Consumption/budgets"
        },
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeploy-Budget.bylaw.ps1 · 79 lines
# Converted from Deploy-Budget.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deploy-Budget' {

    Properties @{
        DisplayName = 'Deploy a default budget on all subscriptions under the assigned scope'
        Description = 'Deploy a default budget on all subscriptions under the assigned scope'
        Category    = 'Budget'
        Version     = '1.1.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter Amount @{
        Default     = '1000'
        Description = 'The total amount of cost or usage to track with the budget'
    }
    Parameter BudgetName @{
        Default     = 'budget-set-by-policy'
        Description = 'The name for the budget to be created'
    }
    Parameter ContactEmails @{
        Type        = 'Array'
        Default     = @()
        Description = 'The list of email addresses, in an array, to send the budget notification to when the threshold is exceeded.'
    }
    Parameter ContactGroups @{
        Type        = 'Array'
        Default     = @()
        Description = 'The list of action groups, in an array, to send the budget notification to when the threshold is exceeded. It accepts array of strings.'
    }
    Parameter ContactRoles @{
        Type        = 'Array'
        Default     = @('Owner', 'Contributor')
        Description = 'The list of contact RBAC roles, in an array, to send the budget notification to when the threshold is exceeded.'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter FirstThreshold @{
        Default     = '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 and 1000.'
    }
    Parameter SecondThreshold @{
        Default     = '100'
        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       = 'Monthly'
        Description   = 'The time covered by a budget. Tracking of the amount will be reset based on the time grain.'
    }

    Resource 'Microsoft.Resources/subscriptions'

    DeployIfNotExists 'Microsoft.Consumption/budgets' -Role '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/Deploy-Budget.json'
            Scope        = 'Subscription'
            Location     = 'northeurope'
            Parameters   = [ordered]@{
                amount          = (Param Amount)
                budgetName      = (Param BudgetName)
                contactEmails   = (Param ContactEmails)
                contactGroups   = (Param ContactGroups)
                contactRoles    = (Param ContactRoles)
                firstThreshold  = (Param FirstThreshold)
                secondThreshold = (Param SecondThreshold)
                timeGrain       = (Param TimeGrain)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: policy_definitions/Deploy-Budget.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.