How To Enforce Compliance Using Azure Policy
In this guide, we will explore how to create Azure Policies to enforce compliance at scale. What is Azure Policy?
Azure Policy helps to enforce organizational standards and to assess compliance at-scale. Through its compliance dashboard, it provides an aggregated view to evaluate the overall state of the environment, with the ability to drill down to the per-resource, per-policy granularity. It also helps to bring your resources to compliance through bulk remediation for existing resources and automatic remediation for new resources.
When you are finished, you will accomplish the following objectives:
- Creating a resource group
- Create a Policy Assignment
- Test the assigned policy
- Azure account with an active subscription. You can create a Free Azure Account if not available.
Task 1 - Creating a resource group
- Sign in to the Azure Portal.
- Click on the Cloud Shell in the Azure Portal top bar.
- If prompted, select Powershell and Create Storage.
- Run the following command to create the resource group;
1$rgName = "enforce-policy-rg" 2$rgLocation = "East US" 3New-AzResourceGroup -Name $rgName -Location $rgLocation
- Once successful, close the Cloud Shell
Task 2 - Create a Policy Assignment
You will use an built-in policy Allowed locations to restrict where the resources can be created.
- In the search bar on Azure Portal, look for Policy.
- In the Authoring section, open Definitions.
- In the search box, look for Allowed locations.
- Choose the definition Allowed locations.
- Go through the policy to understand what will happen post assignment.
- Click Assign
- On the Basics tab, click the ellipsis (...) icon.
- In the Scope blade, fill the details:
|Subscription||the name of your Azure subscription|
|Resource group||the name of created resource group|
- Keep the rest of the settings as default. Click Next.
- In the Allowed Locations dropdown, select the West US 2 region and click Review + Create. Finally click on Create.
Task 3 - Test the assigned policy
- In the search bar on Azure Portal, look for Virtual Networks.
- Select + New to create a virtual network.
- Fil the details:
- Click on Review + Create.
if the validation doesn't fail. Wait for few more minutes, it takes sometime for policy to take effect
- Click on the Validation failed message and see the detailed message it was disallowed by the policy.
- Go back to the Basics tab and change the location to West US 2.
- Click on Review + Create, this time the validation will pass. Click on Create to create the virtual network.
Remove unwanted resource to not get billed by unexpected resource usage.
- Open the Cloud Shell and choose Powershell. Run the command:
1$rgName = "enforce-policy-rg" 2Remove-AzResourceGroup -Name $rgName -Force -AsJob
You learned how to assign a built-in policy definition to the resource group. Policy only allows to create resources in the specified region, any other region selected while creating resources will result in failed validation. This helps the organization to enforce compliance and maintain standard across the Azure account.