Bylaw

Gallery › Storage › Azure Community Policy

Deploy 'Geo-redundant' replication on Storage Account

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

This policy set geo-redundancy on storage accounts.

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

Azure Policy JSONazurepolicy.json · 103 lines
{
  "name": "1acd1d5a-5d92-4c21-bbb9-d10f91bc9102",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy 'Geo-redundant' replication on Storage Account",
    "description": "This policy set geo-redundancy on storage accounts.",
    "metadata": {
      "category": "Storage",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Storage/storageAccounts"
          },
          {
            "field": "Microsoft.Storage/storageAccounts/sku.name",
            "notIn": [
              "Standard_RAGRS",
              "Standard_GRS",
              "Standard_GZRS",
              "Standard_RAGZRS"
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Storage/storageAccounts",
          "name": "[field('name')]",
          "existenceCondition": {
            "field": "Microsoft.Storage/storageAccounts/sku.name",
            "in": [
              "Standard_RAGRS",
              "Standard_GRS",
              "Standard_GZRS",
              "Standard_RAGZRS"
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/17d1049b-9a84-46fb-8f53-869881c3d3ab"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "storageAccountName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "type": "Microsoft.Storage/storageAccounts",
                    "apiVersion": "2019-04-01",
                    "name": "[parameters('storageAccountName')]",
                    "location": "[parameters('location')]",
                    "sku": {
                      "name": "Standard_RAGRS",
                      "tier": "Standard"
                    }
                  }
                ]
              },
              "parameters": {
                "storageAccountName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw1acd1d5a-5d92-4c21-bbb9-d10f91bc9102.bylaw.ps1 · 35 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '1acd1d5a-5d92-4c21-bbb9-d10f91bc9102' {

    Properties @{
        DisplayName = 'Deploy ''Geo-redundant'' replication on Storage Account'
        Description = 'This policy set geo-redundancy on storage accounts.'
        Category    = 'Storage'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Storage/storageAccounts' {

        Require 'sku.name is one of 4 values' {
            Field sku.name | Must -BeIn @('Standard_RAGRS', 'Standard_GRS', 'Standard_GZRS', 'Standard_RAGZRS')
        }
    }

    DeployIfNotExists 'Microsoft.Storage/storageAccounts' -Role 'Storage Account Contributor' -Name (Arm '[field(''name'')]') -Effect (Param Effect) {
        Require 'sku.name is one of 4 values' {
            Field sku.name | Must -BeIn @('Standard_RAGRS', 'Standard_GRS', 'Standard_GZRS', 'Standard_RAGZRS')
        }
        Deployment -TemplateFile './templates/1acd1d5a-5d92-4c21-bbb9-d10f91bc9102.json' -Parameters ([ordered]@{
                storageAccountName = (Arm '[field(''name'')]')
                location           = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Storage/deploy-'geo-redundant'-replication-on-storage-account/azurepolicy.json in Azure/Community-Policy, under the MIT licence.