{
    "type": "Microsoft.Authorization/policyDefinitions",
    "name": "custom-image-from-rg",
    "properties": {
        "displayName": "Allow custom VM image from a Resource Group",
        "description": "This policy allows only usage of images from a resource group",
        "parameters": {
            "resourceGroupName":{
                "type":"String",
                "metadata":{
                    "displayName": "Resource Group Name",
                    "strongType": "ExistingResourceGroups"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [
                    {
                        "field": "type",
                        "equals": "Microsoft.Compute/virtualMachines"
                    },
                    {
                        "not": {
                            "field": "Microsoft.Compute/imageId",
                            "contains": "[concat('resourceGroups/',parameters('resourceGroupName'))]"
                        }
                    }
                ]
            },
            "then": {
                "effect": "deny"
            }
        }
    }
}