Bylaw

Gallery › Cost Optimization › Azure Community Policy

Unused Disks driving cost should be avoided

Converted Policy Audit Disabled Mode All Version 1.0.0

Optimize cost by detecting unused but chargeable resources. Leverage this Policy definition as a cost control to reveal orphaned Disks that are driving cost.

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

Azure Policy JSONazurepolicy.json · 60 lines
{
  "name": "055b1d22-872e-4ba9-bf93-c6cff8b8d8cb",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Unused Disks driving cost should be avoided",
    "description": "Optimize cost by detecting unused but chargeable resources. Leverage this Policy definition as a cost control to reveal orphaned Disks that are driving cost.",
    "metadata": {
      "version": "1.0.0",
      "category": "Cost Optimization"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/disks"
          },
          {
            "field": "Microsoft.Compute/disks/diskState",
            "equals": "Unattached"
          },
          {
            "allof": [
              {
                "field": "name",
                "notlike": "*-ASRReplica"
              },
              {
                "field": "name",
                "notlike": "ms-asr-*"
              },
              {
                "field": "name",
                "notlike": "asrseeddisk-*"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw055b1d22-872e-4ba9-bf93-c6cff8b8d8cb.bylaw.ps1 · 33 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '055b1d22-872e-4ba9-bf93-c6cff8b8d8cb' {

    Properties @{
        DisplayName = 'Unused Disks driving cost should be avoided'
        Description = 'Optimize cost by detecting unused but chargeable resources. Leverage this Policy definition as a cost control to reveal orphaned Disks that are driving cost.'
        Version     = '1.0.0'
        Category    = 'Cost Optimization'
        Mode        = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/disks' {

        When {
            Field diskState | Must -Be 'Unattached'
            Field name | Must -Not -BeLike '*-ASRReplica'
            Field name | Must -Not -BeLike 'ms-asr-*'
        }

        Require 'name is like asrseeddisk-*' {
            Field name | Must -BeLike 'asrseeddisk-*'
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Cost Optimization/unused-disks-driving-cost-should-be-avoided/azurepolicy.json in Azure/Community-Policy, under the MIT licence.