In this exercise, you will:
But before that, here's a recap of key concepts related to Azure Policy:
Azure Administrators use Azure Policy to create policies that define conventions for resources.
There are four basic steps to create and work with policy definitions:
Now let's get back to action!
Here's what you'll see in the Azure portal:
Let's create your first management group in Azure.
1. Click the Create button, assign an unique Management Group ID (e.g. mymanagementgroupID), and an unique Management group display name (e.g. My Management Group).
2. Wait for Azure to create the new management group - it might take up to a minute before the Azure portal finally refreshes with your fancy new group inside!
The first step in enforcing compliance with Azure Policy is to assign a policy definition. In this example, assign the built-in policy definition called Inherit a tag from the resource group if missing.
Can you decipher what this policy does based on its name? No worries if not! This policy adds automatically tags a resource if you don't create/update it with a tag yourself. The resource will have the exact same tag as its resource group.
1. Go to the Azure portal to assign policies. Search for and select Policy.
2. Select Assignments on the left side of the Azure Policy page. An assignment is a policy applied to a specific scope e.g. to the resource level, resource group level, management group level.
3. Select Assign Policy from the top of the Policy - Assignments page.
4. On the Assign Policy page's Basics tab, select the Scope by selecting the ellipsis and selecting your management group - it might already be selected for you. Select Select at the bottom of the Scope page.
5. Exclusions start at one level lower than the level of the Scope. Exclusions are optional, so let's leave it blank for now.
6. Select the Policy definition ellipsis to open the list of available definitions. You can filter the policy definition Type to Built-in to view all and read their descriptions.
7. Select Inherit a tag from the resource group if missing. If you can't find it right away, type inherit a tag into the search box and then press ENTER or select out of the search box. Select Add at the bottom of the Available Definitions page once you have found and selected the policy definition.
8. The Assignment name is automatically populated with the policy name you selected, but you can change it. For this example, leave Inherit a tag from the resource group if missing. You can also add an optional Description. The description provides details about this policy assignment.
9. Leave Policy enforcement as Enabled. Disabling it allows you to simulate the policy's impact without taking real actions on resources.
10. Assigned by is automatically filled based on who is logged in. This field is optional, so custom values can be entered.
11. Select the Parameters tab at the top of the wizard.
12. For Tag Name, enter Environment.
13. Select the Remediation tab at the top of the wizard.
14. Leave Create a remediation task unchecked. This box allows you to create a task to alter existing resources in addition to new or updated resources.
15. Select the Non-compliance messages tab at the top of the wizard.
16. Set the Non-compliance message to This resource doesn't have the required tag. This custom message is displayed when a resource is denied or for non-compliant resources during regular evaluation.
17. Select the Review + create tab at the top of the wizard.
18. Review your selections, then select Create at the bottom of the page.
Let's see your first policy in action. This time, you will see what happens if we forgot to add a tag to a newly created resource.
3. Select Review, wait Azure to run its validation, then click Create.
4. After creating a new resource, go to Policy in the search bar, then to Compliance. You should see a non-compliant resource.
Nice! That's Azure Policy's power in helping you evaluate the compliance of your resources against the rules you've set up.
Now that you've assigned a built-in policy definition, you can do more with Azure Policy. Next, create a new custom policy to save costs by validating that virtual machines created in your environment can't be in the G series*. This way, every time a user in your organisation tries to create a virtual machine in the G series, the request is denied.
*The G series in Azure is a category of powerful virtual machines with high computing power and memory, making them suitable for demanding tasks. An organisation would want to make sure that people don't accidentally use these powerful but potentially expensive virtual machines.
1. Select Definitions under Authoring in the left side of the Azure Policy page.
2. Select + Policy definition at the top of the page. This button opens to the Policy definition page.
3. Enter the following information:
Note: If you plan to apply this policy definition to multiple subscriptions, the location must be a management group that contains the subscriptions you assign the policy to. The same is true for an initiative definition.
4. Write your policy using JSON, specifying
Here's what the JSON should look like:
5. Select Save.
1. Select Definitions under Authoring in the left side of the Azure Policy page.
2. Select + Initiative Definition at the top of the page to open the Initiative definition wizard.
3. Use the Initiative location ellipsis to select a management group or subscription to store the definition. If the previous page was scoped to a single management group or subscription, Initiative location is automatically populated.
4. Enter the Name and Description of the initiative.
5. This example validates that resources are in compliance with policy definitions about getting secure. Name the initiative Get Secure and set the description as: This initiative has been created to handle all policy definitions associated with securing resources.
6. For Category, choose from existing options or create a new category.
7. Set a Version for the initiative, such as 1.0.
8. Select Next at the bottom of the page or the Policies tab at the top of the wizard.
9. Select Add policy definition(s) button and browse through the list. Select the policy definition(s) you want added to this initiative. For the Get Secure initiative, add the following built-in policy definitions by selecting the checkbox next to the policy definition:
10. After selecting each policy definition from the list, select Add at the bottom of the list. Since it's added twice, the Add or replace a tag on resources policy definitions each get a different reference ID.
Note: The selected policy definitions can be added to groups by selecting one or more added definitions and selecting Add selected policies to a group. The group must exist first and can be created on the Groups tab of the wizard.
11. Select Next at the bottom of the page or the Groups tab at the top of the wizard. New groups can be added from this tab. For this tutorial, we aren't adding any groups.
12. Select Next at the bottom of the page or the Initiative parameters tab at the top of the wizard. If we wanted a parameter to exist at the initiative for passing to one or more included policy definitions, the parameter is defined here and then used on the Policy parameters tab. For this tutorial, we aren't adding any initiative parameters.
Note: Once saved to an initiative definition, initiative parameters can't be deleted from the initiative. If an initiative parameter is no longer needed, remove it from use by any policy definition parameters.
13. Select Next at the bottom of the page or the Policy parameters tab at the top of the wizard.
14. Policy definitions added to the initiative that have parameters are displayed in a grid.
The "value type" can be one of three options: 'Default value,' 'Set value,' or 'Use Initiative Parameter.' If you choose 'Set value,' you need to enter the relevant value in the 'Value(s)' field. If the policy definition includes a list of allowed values for a parameter, the entry box will be a dropdown list. If you opt for 'Use Initiative Parameter,' a dropdown list will appear with the names of initiative parameters that you've created on the 'Initiative parameters' tab.
15. Set the 'Allowed locations' value type to 'Set value' and select 'East US 2' from the dropdown list.
16. For the two instances of the Add or replace a tag on resources policy definitions, set the Tag Name parameters to 'Env' and 'CostCenter and the Tag Value parameters to 'Test' and 'Lab' as shown below.
17. Leave the others as 'Default value'.
This configuration adds or updates an 'Env' tag to 'Test' and a 'CostCenter' tag to 'Lab' for resources covered by the assignment.
18. Select Review + create at the bottom of the page or at the top of the wizard.
19. Review the settings and select Ceate.
Nice - you have your policies defined, your initiative definition created, and your policies assigned to affected resources.
The last step is to evaluate the state of compliance for your scoped resources. We have seen this a while ago in the previous task.
The following example shows how you can use the Compliance feature to look for non-compliant initiatives, policies, and resources.
1. Go to the Compliance tab under Policy to check the report as shown:
Azure Policy has evaluated your resources against the policy initiatives you've set up! Each resource is either compliant or non-compliant. You're now ready to identify non-compliant resources to understand the compliance state of your environment.
1. Select Compliance in the left side of the page. Then locate Audit VMs that do not use managed disks policy assignment you created.
2. If there are any existing resources that aren't compliant with this new assignment, they appear under Non-compliant resources.
To remove the assignment created, follow these steps:
You created a Management Group, an Azure policy, and checked its compliance. Keep it up chief!
This table outlines how various policy actions determine whether a resource is compliant or non-compliant based on different conditions:
Note: The DeployIfNotExist and AuditIfNotExist effects require the IF statement to be TRUE and the existence condition to be FALSE to be non-compliant. When TRUE, the IF condition triggers evaluation of the existence condition for the related resources.
For "DeployIfNotExist" and "AuditIfNotExist" to say a resource is non-compliant, two things must happen:
So, if both of these are true, it means your resource is not compliant. It checks if the thing it's asking for is there or not when the initial condition is true.