Bylaw

Gallery › Cost Optimization › Azure Landing Zones Library

Unused Public IP addresses driving cost should be avoided

Converted Policy Audit Disabled Mode All Version 1.1.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 JSONAudit-PublicIpAddresses-UnusedResourcesCostOptimization.alz_policy_definition.json · 87 lines
{
  "name": "Audit-PublicIpAddresses-UnusedResourcesCostOptimization",
  "properties": {
    "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.",
    "displayName": "Unused Public IP addresses driving cost should be avoided",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Cost Optimization",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.1.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "allowedValues": [
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit",
        "metadata": {
          "description": "Enable or disable the execution of the policy",
          "displayName": "Effect"
        },
        "type": "String"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "microsoft.network/publicIpAddresses",
            "field": "type"
          },
          {
            "equals": "Static",
            "field": "Microsoft.Network/publicIPAddresses/publicIPAllocationMethod"
          },
          {
            "anyOf": [
              {
                "exists": false,
                "field": "Microsoft.Network/publicIPAddresses/natGateway"
              },
              {
                "equals": true,
                "value": "[equals(length(field('Microsoft.Network/publicIPAddresses/natGateway')), 0)]"
              }
            ]
          },
          {
            "anyOf": [
              {
                "exists": false,
                "field": "Microsoft.Network/publicIPAddresses/ipConfiguration"
              },
              {
                "equals": true,
                "value": "[equals(length(field('Microsoft.Network/publicIPAddresses/ipConfiguration')), 0)]"
              }
            ]
          },
          {
            "anyOf": [
              {
                "exists": false,
                "field": "Microsoft.Network/publicIPAddresses/publicIPPrefix"
              },
              {
                "equals": true,
                "value": "[equals(length(field('Microsoft.Network/publicIPAddresses/publicIPPrefix')), 0)]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawAudit-PublicIpAddresses-UnusedResourcesCostOptimization.bylaw.ps1 · 44 lines
# Converted from Audit-PublicIpAddresses-UnusedResourcesCostOptimization.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Audit-PublicIpAddresses-UnusedResourcesCostOptimization' {

    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.'
        Category    = 'Cost Optimization'
        Version     = '1.1.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

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

    Resource 'microsoft.network/publicIpAddresses' {

        When {
            Field publicIPAllocationMethod | Must -Be 'Static'
            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: policy_definitions/Audit-PublicIpAddresses-UnusedResourcesCostOptimization.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.