Bylaw

Gallery › Network › Azure Community Policy

Deny VNet creation outside of Resource Groups based on a naming standard

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This Policy will deny the creation of VNets outside of Resource Groups that follow a certain 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": "1f81b2f1-8cb2-402e-836c-1b6fcbdd641c",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deny VNet creation outside of Resource Groups based on a naming standard",
    "description": "This Policy will deny the creation of VNets outside of Resource Groups that follow a certain naming convention.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "resourceGroupNamingConvention": {
        "type": "String",
        "metadata": {
          "displayName": "resourceGroupNamingConvention",
          "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 location 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": [
          {
            "value": "[resourceGroup().name]",
            "notEquals": "[concat(field('location'), parameters('resourceGroupNamingConvention'))]"
          },
          {
            "field": "type",
            "equals": "Microsoft.Network/virtualNetworks"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw1f81b2f1-8cb2-402e-836c-1b6fcbdd641c.bylaw.ps1 · 32 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '1f81b2f1-8cb2-402e-836c-1b6fcbdd641c' {

    Properties @{
        DisplayName = 'Deny VNet creation outside of Resource Groups based on a naming standard'
        Description = 'This Policy will deny the creation of VNets outside of Resource Groups that follow a certain naming convention.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter ResourceGroupNamingConvention @{
        Type        = 'String'
        DisplayName = 'resourceGroupNamingConvention'
        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 location 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 'location is as required' {
            Arm '[resourceGroup().name]' | Must -Be (Arm '[concat(field(''location''), parameters(''resourceGroupNamingConvention''))]')
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-vnet-creation-outside-of-resource-groups-based-on-a-naming-standard/azurepolicy.json in Azure/Community-Policy, under the MIT licence.