Bylaw

Gallery › Tags › Azure built-in policies

Require a tag and its value on resources

Converted Policy Deny Mode Indexed Version 1.0.1

Enforces a required tag and its value. Does not apply to resource groups.

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

Azure Policy JSONRequireTagAndValue_Deny.json · 45 lines
{
  "properties": {
    "displayName": "Require a tag and its value on resources",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Enforces a required tag and its value. Does not apply to resource groups.",
    "metadata": {
      "version": "1.0.1",
      "category": "Tags"
    },
    "version": "1.0.1",
    "parameters": {
      "tagName": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Name",
          "description": "Name of the tag, such as 'environment'"
        }
      },
      "tagValue": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Value",
          "description": "Value of the tag, such as 'production'"
        }
      }
    },
    "policyRule": {
      "if": {
        "not": {
          "field": "[concat('tags[', parameters('tagName'), ']')]",
          "equals": "[parameters('tagValue')]"
        }
      },
      "then": {
        "effect": "deny"
      }
    },
    "versions": [
      "1.0.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/1e30110a-5ceb-460c-a204-c1c3969c6d62",
  "name": "1e30110a-5ceb-460c-a204-c1c3969c6d62"
}
Bylaw1e30110a-5ceb-460c-a204-c1c3969c6d62.bylaw.ps1 · 32 lines
# Converted from RequireTagAndValue_Deny.json by ConvertTo-Bylaw.
Policy '1e30110a-5ceb-460c-a204-c1c3969c6d62' {

    Properties @{
        DisplayName   = 'Require a tag and its value on resources'
        Description   = 'Enforces a required tag and its value. Does not apply to resource groups.'
        Version       = '1.0.1'
        Category      = 'Tags'
        PolicyVersion = '1.0.1'
        Mode          = 'Indexed'
    }

    Parameter TagName @{
        Type        = 'String'
        DisplayName = 'Tag Name'
        Description = 'Name of the tag, such as ''environment'''
    }
    Parameter TagValue @{
        Type        = 'String'
        DisplayName = 'Tag Value'
        Description = 'Value of the tag, such as ''production'''
    }

    Resource * {

        Require 'tag (Param TagName) is the TagValue parameter' {
            Tag (Param TagName) | Must -Be (Param TagValue)
        }
    }

    Effect Deny
}

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