Assign a policy that limits regions
In this task you'll assign a policy that limits which regions can be used.
Azure Policy checks resources against rules, and can block anything that breaks them. The built-in Allowed locations policy stops anyone creating resources outside the regions you choose.
A policy applies to its scope and everything below it, for everyone, Owners included. Resources that already exist aren't deleted; they're just reported as not compliant.
Example. SS Kaiser must keep its customer data in the UK, so it assigns Allowed locations with UK South and UK West to its production subscription. Nobody can create a database in another region by mistake.
Before you start
You need: Owner or Resource Policy Contributor. Time: 10 minutes, plus waiting.
Where to look
- Home
- Policy
- Assignments
- Assign policy
Steps
- Search for policy and open Policy.
- Select Assignments in the left menu, then Assign policy.
- Next to Scope, select the … button, choose your subscription and the resource group rg-portal-practice, then Select.Practising on one group keeps the rule away from everything else.
- Next to Policy definition, select the … button, search for Allowed locations, select it and choose Add.There's also Allowed locations for resource groups, which checks the groups themselves rather than what goes in them.
- Select Next until you reach Parameters, and under Allowed locations choose one region, such as UK South.
- Select Review + create, then Create.
- Wait a few minutes (it can take up to half an hour, though in our test it worked within a minute), then try to create a free resource in the group in another region, such as a virtual network in North Europe. The final check should fail with RequestDisallowedByPolicy.Nothing is created, because the policy blocks it. That's the test passing.
- Open Compliance to see the assignment and whether anything breaks the rule.Results for a new assignment can take a while to appear.
Commands
# Find the built-in definition's name (a long ID)
DEF=$(az policy definition list --query "[?displayName=='Allowed locations'].name" --output tsv)
# Assign it to the practice group, allowing UK South only
az policy assignment create --name allowed-locations-practice --policy $DEF \
--resource-group rg-portal-practice \
--params '{ "listOfAllowedLocations": { "value": ["uksouth"] } }'
# Remove it again
az policy assignment delete --name allowed-locations-practice --resource-group rg-portal-practice
Watch out for
- Cost
- Azure Policy is free for checking Azure resources.
- Security
- Policies apply to everyone, so they make strong guard rails. Anyone who can delete the assignment can lift the rule, so keep Owner for very few people.
- Common mistakes
- Picking Allowed locations for resource groups when you meant the resources, or the other way round. Some services are global and have no region; this policy lets those through. Existing resources in other regions stay where they are.
Clean up
- Policy
- Assignments
- Allowed locations
- Delete assignment
Open the assignment and select Delete assignment, then confirm. Do this before later sections, or the rule will block resources you create in other regions. If you delete the resource group first, check Assignments afterwards and delete it there if it's still listed.
Words to know
- Policy definition
- The rule itself, such as Allowed locations.
- Assignment
- A rule applied to a scope, with your own settings, such as which regions are allowed.
- Compliance
- Whether the resources in a scope follow the rules assigned to it.
Tick off each step and track your progress in the guide.
Open this task in Azure Portal, Hands On