To create a custom policy to restrict the maximum number of VMs in a specific region:
- In the Azure Portal, navigate to the Azure Policy service.
- Click on the "+ Policy definition" button to create a new policy definition.
- In the "Basics" tab, give the policy a name and select "Custom" as the policy type.
- In the "Policy rule" tab, you can use the built-in policy function "count" to check the number of VMs in a specific region. You can use a condition like this:
Copy code count(resourceId, 'Microsoft.Compute/virtualMachines', 'West US') > 10
This will check for more than 10 VMs in the region "West US"
- In the "Effect" tab, select "Deny" to prevent the creation of new VMs in that region.
- Click on the "Create" button to create the policy.
- After creating the policy, you need to assign it to the management group or subscription where you want it to take effect.
You can use Azure Policy initiative to manage multiple policies at once.
Also, you can use Azure Quotas to restrict the number of resources that can be created in a subscription, by using Azure Quotas you can limit the number of VMs that can be created in a subscription, and you can also assign different quotas to different subscriptions or management groups.