Bylaw

Gallery › General › Azure built-in policies

Audit resource location matches resource group location

Converted Policy Audit Deny Disabled Mode Indexed Version 2.1.0

Audit that the resource location matches its resource group location

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

Azure Policy JSONResourcesInResourceGroupLocation_Audit.json · 51 lines
{
  "properties": {
    "displayName": "Audit resource location matches resource group location",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Audit that the resource location matches its resource group location",
    "metadata": {
      "version": "2.1.0",
      "category": "General"
    },
    "version": "2.1.0",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "location",
            "notEquals": "[resourcegroup().location]"
          },
          {
            "field": "location",
            "notEquals": "global"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "2.1.0",
      "2.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/0a914e76-4921-4c19-b460-a2d36003525a",
  "name": "0a914e76-4921-4c19-b460-a2d36003525a"
}
Bylaw0a914e76-4921-4c19-b460-a2d36003525a.bylaw.ps1 · 32 lines
# Converted from ResourcesInResourceGroupLocation_Audit.json by ConvertTo-Bylaw.
Policy '0a914e76-4921-4c19-b460-a2d36003525a' {

    Properties @{
        DisplayName   = 'Audit resource location matches resource group location'
        Description   = 'Audit that the resource location matches its resource group location'
        Version       = '2.1.0'
        Category      = 'General'
        PolicyVersion = '2.1.0'
        Mode          = 'Indexed'
    }

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

    Resource * {

        When {
            Field location | Must -Not -Be (Arm '[resourcegroup().location]')
        }

        Require 'location is global' {
            Field location | Must -Be 'global'
        }
    }

    Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/General/ResourcesInResourceGroupLocation_Audit.json in Azure/azure-policy, under the MIT licence.