Bylaw

Gallery › Network › Azure Community Policy

Deny creation of VNets that don't follow a pre-defined naming convention

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This Policy will deny the creation of VNets that do not follow a pre-defined naming convention.

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

Azure Policy JSONazurepolicy.json · 52 lines
{
  "name": "578e87f4-56f8-4fae-9aab-4efe51566f0b",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deny creation of VNets that don't follow a pre-defined naming convention",
    "description": "This Policy will deny the creation of VNets that do not follow a pre-defined naming convention.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "vnetNamingConvention": {
        "type": "String",
        "metadata": {
          "displayName": "Virtual Network Naming Convention",
          "description": "The policy will concat this value with the provisioned resources location. Example, if you provide the value of '-example' the concat result will be like: 'eastus-example'. If the created resources name does not match this contact then Policy will deny the creation."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/virtualNetworks"
          },
          {
            "field": "name",
            "notEquals": "[concat(field('location'), parameters('vnetNamingConvention'))]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw578e87f4-56f8-4fae-9aab-4efe51566f0b.bylaw.ps1 · 32 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '578e87f4-56f8-4fae-9aab-4efe51566f0b' {

    Properties @{
        DisplayName = 'Deny creation of VNets that don''t follow a pre-defined naming convention'
        Description = 'This Policy will deny the creation of VNets that do not follow a pre-defined naming convention.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter VnetNamingConvention @{
        Type        = 'String'
        DisplayName = 'Virtual Network Naming Convention'
        Description = 'The policy will concat this value with the provisioned resources location. Example, if you provide the value of ''-example'' the concat result will be like: ''eastus-example''. If the created resources name does not match this contact then Policy will deny the creation.'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/virtualNetworks' {

        Require 'name is (Arm ''[concat(field(''''location''''), parameters(''''vnetNamingConvention''''))]'')' {
            Field name | Must -Be (Arm '[concat(field(''location''), parameters(''vnetNamingConvention''))]')
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-creation-of-vnets-that-don't-follow-a-pre-defined-naming-convention/azurepolicy.json in Azure/Community-Policy, under the MIT licence.