Bylaw
Free and open source · MIT licence

Azure Policy you can read.

Bylaw is a PowerShell module for writing Azure Policy in a Pester-style language. You describe what a compliant resource looks like, and Bylaw compiles it into Azure Policy definition JSON, for any tool that deploys policy definitions.

Get started Read the documentation Install-PSResource -Name Bylaw -Prerelease
You writestorage-require-tls12.bylaw.ps1
Policy 'storage-require-tls12' {

    Properties @{
        DisplayName = 'Storage accounts should require TLS 1.2'
        Category    = 'Storage'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Resource 'Microsoft.Storage/storageAccounts' {
        Require 'TLS 1.2 or later' {
            Field minimumTlsVersion | Must -Be 'TLS1_2'
        }
    }

    Effect Deny
}
Bylaw compilesstorage-require-tls12.json
{
  "name": "storage-require-tls12",
  "properties": {
    "displayName": "Storage accounts should require TLS 1.2",
    "mode": "Indexed",
    "metadata": {
      "version": "1.0.0",
      "category": "Storage"
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Storage/storageAccounts"
          },
          {
            "field": "Microsoft.Storage/storageAccounts/minimumTlsVersion",
            "notEquals": "TLS1_2"
          }
        ]
      },
      "then": {
        "effect": "Deny"
      }
    }
  }
}

You say what a compliant storage account has: minimumTlsVersion of TLS1_2. Bylaw writes the if that finds the accounts breaking it, with the field's full alias, checked against Azure's aliases when you build. In Azure, notEquals also matches an account without the setting, so that one is denied too.

Why Bylaw

You write what must be true

Bylaw works out the if condition that finds resources breaking your rule, including the negations and missing properties that are easy to get wrong by hand.

Short field names, checked when you build

minimumTlsVersion becomes its full alias, and a misspelling fails the build instead of producing a policy that never matches.

Checks across the whole definition

Arrays, remediation, related resources and initiatives are checked against the rule and the policies they use, before anything is deployed.

The same input, the same files

Output is deterministic, so a pull request's diff shows only real changes to your policies.

Existing policies convert

ConvertTo-Bylaw turns your current JSON into Bylaw files, and proves that each one compiles back to the same rule.

It's PowerShell

Loops, data files and helper functions can generate policies, and every Azure Policy effect, count and initiative is covered.

Bylaw only compiles. Deploy its output with Enterprise Policy as Code, Bicep, Terraform, the Azure CLI, or any tool that takes policy definition files.