Lab 02: Set guardrails with Azure Policy¶
At a glance
- Time: about 60 minutes, including about 30 minutes of waiting
- Where: your policy sandbox,
rg-CLOUD-LAB-GRP-N-policy(not your lab resource group) - You create: 2 free network security groups, 3 policy assignments and 1 remediation task
- Clean up: delete your 3 policy assignments at the end
What you will learn¶
- What Azure Policy does and how it differs from RBAC (Lab 01).
- Three policy effects: Deny (block), Audit (report) and Modify (change a resource).
- How to read compliance, and how to remediate resources that already exist.
Why a sandbox?
Your lab resource group already has the lab guardrails assigned to it, and you can't change them. To practise assigning policies safely, you get a second resource group, your policy sandbox. You can assign and delete policies there. The policies you create only affect the sandbox, never your lab resource group.
Before you start¶
- You are signed in to the lab directory (Getting started).
- You know your group number
N. In this lab, replaceNwith it. - In Resource groups, you can see two groups:
rg-CLOUD-LAB-GRP-N(your lab) andrg-CLOUD-LAB-GRP-N-policy(your sandbox).
Task 1: Look at your sandbox¶
- Open the resource group
rg-CLOUD-LAB-GRP-N-policy. - On the Overview page, find the Tags. Note the tag
CostCenterwith the valueLAB-GRP-N. You use it later. - You see one resource,
id-policy-remediation. It is a managed identity that a policy uses to change resources for you (Task 4). - Open Access control (IAM) → View my access. Compare your role here,
Lab Student Policy Sandbox, withLab Studentfrom Lab 01.
Task 2: Create two test resources¶
Policies need something to evaluate. You create two network security groups (NSGs). They are free.
- In your sandbox, select + Create and search for Network security group.
-
Create the first NSG:
Setting Value Resource group rg-CLOUD-LAB-GRP-N-policyName nsg-N-seaRegion Southeast Asia -
Select Review + create, then Create. Don't add any tags.
- Create the second NSG the same way, but name it
nsg-N-sea3and set the region to Southeast Asia 3.
Task 3: Assign the Deny and Audit policies¶
Starting these now lets Azure begin working while you continue.
Deny: allowed locations¶
- Search for Policy and select it, then select Authoring → Assignments.
- Select Assign policy.
- On Basics:
- Scope: select … and choose your sandbox,
rg-CLOUD-LAB-GRP-N-policy. - Policy definition: select … and search for Allowed locations. Select it.
- Assignment name:
allowed-locations-N
- Scope: select … and choose your sandbox,
- On Parameters, clear Only show parameters that need input or review. Set:
- Allowed locations:
Southeast AsiaandSoutheast Asia 3 - Effect:
Deny
- Allowed locations:
- Select Review + create, then Create.
Audit: location matches the resource group¶
Repeat the steps above with these values:
| Setting | Value |
|---|---|
| Scope | rg-CLOUD-LAB-GRP-N-policy |
| Policy definition | Audit resource location matches resource group location |
| Assignment name | audit-location-N |
| Effect | Audit |
Waiting time
A new assignment can take up to 30 minutes to take effect. Carry on with Tasks 4 and 5 and come back to Task 6 afterwards.
Task 4: Assign the Modify policy¶
A Modify policy changes a resource, here by adding a tag. It needs an identity with permission to do that. You use the prepared identity.
- In Policy → Assignments, select Assign policy.
- On Basics:
- Scope:
rg-CLOUD-LAB-GRP-N-policy - Policy definition: Inherit a tag from the resource group if missing
- Assignment name:
inherit-costcenter-N
- Scope:
- On Parameters, set Tag Name to
CostCenter. - On Remediation:
- Make sure the effect is Modify.
- Under Managed Identity, select User assigned managed identity.
- Select Existing, and choose
id-policy-remediation. - Leave Create a remediation task cleared. You create it yourself in Task 7.
- Select Review + create, then Create.
Don't choose a system-assigned identity
A system-assigned identity needs a role assignment, which you aren't allowed to create. If you choose it, the assignment fails with an authorization error.
Task 5: Explore the lab guardrails¶
While Azure applies your policies, look at the policies that protect your lab resource group.
- In Policy → Assignments, change the Scope filter to your lab resource group,
rg-CLOUD-LAB-GRP-N. - Select the assignment Lab cost guardrails. Select View definition.
- Look at the list of policies. Each has a code and a title, such as
P01A-REGIONorP03A-VM-SIZE. These are the codes in the[LAB-...]messages. - Select one definition, for example
P03B-VM-SPOT, and open its Definition tab. Find theeffect, and theifcondition that decides when it applies.
Think about it
The lab guardrails use Deny. Why is that a better fit than Audit for keeping costs down?
Trigger a guardrail on purpose¶
- Go to your lab resource group, not the sandbox, and select + Create.
- Search for Network security group and select it.
- Set the region to East US and select Review + create.
- The validation fails. Open the error details and read the message. It starts with
[LAB-P01A-REGION]. Nothing is created.
Task 6: See the compliance results¶
You can start this once about 30 minutes have passed since Task 3.
- In Policy, select Compliance. Set the Scope to your sandbox.
- For each of your three assignments, find the Compliance state.
- Select audit-location-N. You should see
nsg-N-sea3as Non-compliant, because it is not in the region of the resource group.nsg-N-seais compliant. - Select inherit-costcenter-N. You should see both NSGs as Non-compliant: they don't have the
CostCentertag.
No results yet?
The first evaluation can take 30 minutes or more. Refresh the page and try again, or continue with Task 7 and come back.
Task 7: Remediate existing resources¶
Modify only changes new or updated resources. To fix the NSGs that already exist, you run a remediation task.
- In Policy, select Remediation, then the Policies to remediate tab. Set the scope to your sandbox.
- Select inherit-costcenter-N, then Remediate.
- Leave the default settings and select Remediate.
- Open the Remediation tasks tab. Wait until the status is Complete. Refresh if needed.
- Open
nsg-N-sea. Under Tags, you should seeCostCenter=LAB-GRP-N, copied from the resource group.
If the task fails
Open the task and read the error. If it mentions authorization, tell your trainer.
Task 8: Test the Deny policy¶
- In your sandbox, select + Create and search for Virtual network.
- Name it
vnet-N-test, set the region to East US, and select Review + create. - The validation fails with
RequestDisallowedByPolicy. Open the details. It names your assignmentallowed-locations-N. - Change the region to Southeast Asia and select Review + create. It passes. You don't need to create it.
Compare this error with the one in Task 5: both are policy denials. The message of the lab guardrails starts with [LAB-...]; yours doesn't, because you didn't write one.
Task 9: Clean up¶
- In Policy → Assignments, set the scope to your sandbox.
- For each of your three assignments, select … → Delete assignment.
- Optional: delete your two test NSGs.
Check your work¶
- You can explain the difference between Deny, Audit and Modify.
- The compliance page showed
nsg-N-sea3as non-compliant for your Audit policy. - The remediation task added the
CostCentertag to your existing NSGs. - A virtual network in East US was blocked by your policy, with an error that names your assignment.
Key points¶
- Azure Policy controls what resources may look like. RBAC controls what people may do.
- Deny blocks a request. Audit only reports. Modify changes the resource.
- New policies take time to start working. Check Compliance to see the result.
- Modify only fixes new or updated resources. A remediation task fixes existing ones.
- A policy assignment applies to its scope and everything inside it.