An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
Hello Sree, it makes total sense to move from “one-off” API calls to a centralized, policy-driven model—especially when you have multiple subscriptions under a management group. Here’s a high-level approach you can take for both diagnostics (logs & metrics) and VM Insights:
Steps to create an Azure Policy to enable Diagnostic Settings for the following resource types at the subscription scope:"
Resources:
-
Microsoft.Web/serverFarms -
Microsoft.DBforMySQL/flexibleServers -
Microsoft.DBforPostgreSQL/flexibleServers -
Microsoft.Sql/servers/databases
Once the resources are created (or if they are already present), follow the steps below for each resource:"
- Navigate to Azure Policy > Definitions > + Policy Definition
- Set the Definition location to your subscription
- Paste the provided JSON into the Policy Rule editor
- Enter the required details such as Display Name, Description, and set the Category to Monitoring
- Add the appropriate Policy Rule JSON for each resource type as provided below
- Click Save
After completing the above steps, proceed to create and assign the Azure Policy at the subscription scope as outlined below:"
- After clicking Save, the policy definition will be validated and the newly created definition will open.
- Click on Assign Policy.
- Select the appropriate Scope and fill in the remaining required fields. Ensure that Policy Enforcement is enabled.
- Click Next → In the Parameters tab, provide the Log Analytics Workspace Resource ID → Click Next.
- Under Remediation, select Create a remediation task and enable System-assigned managed identity.
- Click Review + Create.
Required Role Assignments:
- The managed identity created for the policy assignment must have the following roles:
- Monitoring Contributor (to configure diagnostic settings)
- Log Analytics Contributor (to send data to the workspace)
- Navigate to Subscriptions → Select your subscription
- Go to Access Control (IAM) → Role Assignments tab
- Filter by Type = Service Principal
- Search using the policy assignment name or principal ID
- Confirm that both required roles are assigned
- Search using the policy assignment name or principal ID
- Filter by Type = Service Principal
- Go to Access Control (IAM) → Role Assignments tab
Note: These steps apply to all specified resource types when creating custom policies to enable diagnostic settings.
- Enable VM insights using Azure Policy (initiatives & assignment) https://docs.microsoft.com/azure/azure-monitor/vm/vminsights-enable-policy
- Assign a VM insights policy initiative https://docs.microsoft.com/azure/azure-monitor/vm/vminsights-enable-policy#assign-a-vm-insights-policy-initiative
- Enable Insights at scale using Azure policies (policy assignment flow) https://docs.microsoft.com/azure/azure-local/manage/monitor-multi-azure-policies?view=azloc-2604
Hope this helps! please feel free to reach out if you have any further questions. Thanks