You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/event-grid/event-schema-policy.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
2
title: Azure Policy as an Event Grid source
3
3
description: This article describes how to use Azure Policy as an Event Grid event source. It provides the schema and links to tutorial and how-to articles.
@@ -63,14 +50,27 @@ az group create --name <resource_group_name> --location westus
63
50
Now that we have a resource group, we create a
64
51
[system topic](../../../event-grid/system-topics.md). A system topic in Event Grid represents one or
65
52
more events published by Azure services such as Azure Policy and Azure Event Hubs. This system topic
66
-
uses the `Microsoft.PolicyInsights.PolicyStates` topic type for Azure Policy state changes. Replace
67
-
`<SubscriptionID>` in the **scope** parameter with the ID of your subscription and
68
-
`<resource_group_name>` in **resource-group** parameter with the previously created resource group.
53
+
uses the `Microsoft.PolicyInsights.PolicyStates` topic type for Azure Policy state changes.
54
+
55
+
First, you'll need to register the `PolicyInsights` and `EventGrid`resource providers (RPs) at the appropriate management scope. Whereas the Azure portal auto-registers any RPs you invoke for the first time, Azure CLI does not.
69
56
70
57
```azurecli-interactive
71
58
# Log in first with az login if you're not using Cloud Shell
72
59
73
-
az eventgrid system-topic create --name PolicyStateChanges --location global --topic-type Microsoft.PolicyInsights.PolicyStates --source "/subscriptions/<subscriptionID>" --resource-group "<resource_group_name>"
60
+
# Register the required RPs at the management group scope
61
+
az provider register --namespace Microsoft.PolicyInsights -m <managementGroupId>
62
+
az provider register --namespace Microsoft.EventGrid -m <managementGroupId>
63
+
64
+
# Alternatively, register the required RPs at the subscription scope (defaults to current subscription context)
65
+
az provider register --namespace Microsoft.PolicyInsights
66
+
az provider register --namespace Microsoft.EventGrid
67
+
```
68
+
69
+
Next, replace `<subscriptionId>` in the **scope** parameter with the ID of your subscription and
70
+
`<resource_group_name>` in **resource-group** parameter with the previously created resource group.
71
+
72
+
```azurecli-interactive
73
+
az eventgrid system-topic create --name PolicyStateChanges --location global --topic-type Microsoft.PolicyInsights.PolicyStates --source "/subscriptions/<subscriptionId>" --resource-group "<resource_group_name>"
74
74
```
75
75
76
76
If your Event Grid system topic will be applied to the management group scope, then the Azure CLI `--source` parameter syntax is a bit different. Here's an example:
@@ -144,7 +144,7 @@ hold the Event Grid topic:
144
144
```azurecli-interactive
145
145
# Log in first with az login if you're not using Cloud Shell
146
146
147
-
az policy assignment create --name 'requiredtags-events' --display-name 'Require tag on RG' --scope '<ResourceGroupScope>' --policy '<policy definition ID>' --params '{ \"tagName\": { \"value\": \"EventTest\" } }'
147
+
az policy assignment create --name 'requiredtags-events' --display-name 'Require tag on RG' --scope '<resourceGroupScope>' --policy '<policy definition ID>' --params '{ \"tagName\": { \"value\": \"EventTest\" } }'
148
148
```
149
149
150
150
The preceding command uses the following information:
@@ -155,7 +155,7 @@ The preceding command uses the following information:
155
155
-**Scope** - A scope determines what resources or grouping of resources the policy assignment gets
156
156
enforced on. It could range from a subscription to resource groups. Be sure to replace
157
157
<scope> with the name of your resource group. The format for a resource group scope is
-**Policy** - The policy definition ID, based on which you're using to create the assignment. In
160
160
this case, it's the ID of policy definition _Require a tag on resource groups_. To get the policy
161
161
definition ID, run this command:
@@ -175,19 +175,26 @@ event notification to appear in the web app. The resource group we created show
175
175
## Trigger a change on the resource group
176
176
177
177
To make the resource group compliant, a tag with the name **EventTest** is required. Add the tag to
178
-
the resource group with the following command replacing `<SubscriptionID>` with your subscription ID
179
-
and `<ResourceGroup>` with the name of the resource group:
178
+
the resource group with the following command replacing `<subscriptionId>` with your subscription ID
179
+
and `<resourceGroup>` with the name of the resource group:
180
180
181
181
```azurecli-interactive
182
182
# Log in first with az login if you're not using Cloud Shell
183
183
184
-
az tag create --resource-id '/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>' --tags EventTest=true
184
+
az tag create --resource-id '/subscriptions/<SubscriptionID>/resourceGroups/<resourceGroup>' --tags EventTest=true
185
185
```
186
186
187
187
After adding the required tag to the resource group, wait for a
188
188
**Microsoft.PolicyInsights.PolicyStateChanged** event notification to appear in the web app. Expand
189
189
the event and the `data.complianceState` value now shows _Compliant_.
190
190
191
+
## Troubleshooting
192
+
193
+
If you see an error similar to one of the following, please make sure that you've registered both resource providers at the scope to which you're subscribing (management group or subscription):
194
+
195
+
-`Deployment has failed with the following error: {"code":"Publisher Notification Error","message":"Failed to enable publisher notifications.","details":[{"code":"Publisher Provider Error","message":"GET request for <uri> failed with status code: Forbidden, code: AuthorizationFailed and message: The client '<identifier>' with object id '<identifier>' does not have authorization to perform action 'microsoft.policyinsights/eventGridFilters/read' over scope '<scope>/providers/microsoft.policyinsights/eventGridFilters/_default' or the scope is invalid. If access was recently granted, please refresh your credentials.."}]}`
196
+
-`Deployment has failed with the following error: {'code':'Publisher Notification Error','message':'Failed to enable publisher notifications.','details':[{'code':'ApiVersionNotSupported','message':'Event Grid notifications are currently not supported by microsoft.policyinsights in global. Try re-registering Microsoft.EventGrid provider if this is your first event subscription in this region.'}]}`
197
+
191
198
## Clean up resources
192
199
193
200
If you plan to continue working with this web app and Azure Policy event subscription, don't clean
0 commit comments