How do you DeployIfNotExists a resource group with azure policy?

Viewed 195

Sample code below. This is

  1. not evaluating correctly
  2. not properly deploying the resource group to all the subscriptions

I'm trying to deploy a policy that deploys resource groups to all subscriptions.

"policyRule": {
  "if": {
    "allOf": [
      {
        "field": "type",
        "equals": "Microsoft.Resources/subscriptions"
      }
    ]
  },
  "then": {
    "effect": "deployIfNotExists",
    "details": {
      "type": "Microsoft.Resources/resourceGroups",
      "evaluationDelay": "AfterProvisioning",
      "roleDefinitionIds": [
        "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
      ],
      "deployment": {
        "location": "canadacentral",
        "properties": {
          "mode": "incremental",
          "template": {
            "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "parameters": {},
            "variables": {},
            "resources": [
              {
                "type": "Microsoft.Resources/resourceGroups",
                "apiVersion": "2021-04-01",
                "name": "myResourceGroup",
                "location": "canadacentral",
                "properties": {}
              }
            ]
          }
        }
      }
    }
  }
}
1 Answers

Something along the lines

"policyRule": {
  "if": {
    "field": "type",
    "equals": "Microsoft.Resources/subscriptions"
  },
  "then": {
    "effect": "deployIfNotExists",
    "details": {
      "type": "Microsoft.Resources/Subscriptions/resourcegroups",
      "roleDefinitionIds": [
        "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
      ],
      "evaluationDelay": "AfterProvisioning",
      "existenceCondition": {
        "field": "name",
        "equals": "myResourceGroup"
      },
      "resourceGroupName": "myResourceGroup",
      "deploymentScope": "subscription",
      "deployment": {
        "location": "canadacentral",
        "properties": {
          "mode": "incremental",
          "template": {
            "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "parameters": {},
            "variables": {},
            "resources": [
              {
                "name": "myResourceGroup",
                "type": "Microsoft.Resources/resourceGroups",
                "apiVersion": "2019-10-01",
                "location": "canadacentral"
              }
            ],
            "outputs": {}
          }
        }
      }
    }
  }
}
Related