Bylaw

Gallery › Cost Optimization › Azure Community Policy

Unused Public IP addresses 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 Public IP addresses that are driving cost.

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

Azure Policy JSONazurepolicy.json · 80 lines
{
  "name": "8ce86601-a13e-4b74-a0ae-f5a0b193860a",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Unused Public IP addresses 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 Public IP addresses 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.network/publicIpAddresses"
          },
          {
            "field": "Microsoft.Network/publicIPAddresses/sku.name",
            "notEquals": "Basic"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Network/publicIPAddresses/natGateway",
                "exists": false
              },
              {
                "value": "[equals(length(field('Microsoft.Network/publicIPAddresses/natGateway')), 0)]",
                "equals": true
              }
            ]
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Network/publicIPAddresses/ipConfiguration",
                "exists": false
              },
              {
                "value": "[equals(length(field('Microsoft.Network/publicIPAddresses/ipConfiguration')), 0)]",
                "equals": true
              }
            ]
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Network/publicIPAddresses/publicIPPrefix",
                "exists": false
              },
              {
                "value": "[equals(length(field('Microsoft.Network/publicIPAddresses/publicIPPrefix')), 0)]",
                "equals": true
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw8ce86601-a13e-4b74-a0ae-f5a0b193860a.bylaw.ps1 · 43 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '8ce86601-a13e-4b74-a0ae-f5a0b193860a' {

    Properties @{
        DisplayName = 'Unused Public IP addresses 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 Public IP addresses 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.network/publicIpAddresses' {

        When {
            Field sku.name | Must -Not -Be 'Basic'
            AnyOf {
                Field natGateway | Must -Not -Exist
                Arm '[equals(length(field(''Microsoft.Network/publicIPAddresses/natGateway'')), 0)]' | Must -Be $true
            }
            AnyOf {
                Field ipConfiguration | Must -Not -Exist
                Arm '[equals(length(field(''Microsoft.Network/publicIPAddresses/ipConfiguration'')), 0)]' | Must -Be $true
            }
        }

        Require 'publicIPPrefix exists' {
            Field publicIPPrefix | Must -Exist
        }

        Require 'publicIPPrefix is as required' {
            Arm '[equals(length(field(''Microsoft.Network/publicIPAddresses/publicIPPrefix'')), 0)]' | Must -Not -Be $true
        }
    }

    Effect (Param Effect)
}

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