Skip to content

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.

Diagram: you assign three policies to your policy sandbox: Deny allowed locations, Audit location matches resource group, and Modify inherit the CostCenter tag. The sandbox holds two network security groups and a managed identity. A virtual network in East US is blocked, the NSG in Southeast Asia 3 is non-compliant for the audit, and a remediation task adds the CostCenter tag. Below, the lab guardrails on your lab resource group block an NSG in East US.
What you assign, where it applies, and what you will see. Select the diagram to open it full size.

Before you start

  • You are signed in to the lab directory (Getting started).
  • You know your group number N. In this lab, replace N with it.
  • In Resource groups, you can see two groups: rg-CLOUD-LAB-GRP-N (your lab) and rg-CLOUD-LAB-GRP-N-policy (your sandbox).

Task 1: Look at your sandbox

  1. Open the resource group rg-CLOUD-LAB-GRP-N-policy.
  2. On the Overview page, find the Tags. Note the tag CostCenter with the value LAB-GRP-N. You use it later.
  3. You see one resource, id-policy-remediation. It is a managed identity that a policy uses to change resources for you (Task 4).
  4. Open Access control (IAM) → View my access. Compare your role here, Lab Student Policy Sandbox, with Lab Student from Lab 01.

Task 2: Create two test resources

Policies need something to evaluate. You create two network security groups (NSGs). They are free.

  1. In your sandbox, select + Create and search for Network security group.
  2. Create the first NSG:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N-policy
    Name nsg-N-sea
    Region Southeast Asia
  3. Select Review + create, then Create. Don't add any tags.

  4. Create the second NSG the same way, but name it nsg-N-sea3 and 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

  1. Search for Policy and select it, then select Authoring → Assignments.
  2. Select Assign policy.
  3. 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
  4. On Parameters, clear Only show parameters that need input or review. Set:
    • Allowed locations: Southeast Asia and Southeast Asia 3
    • Effect: Deny
  5. 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.

  1. In Policy → Assignments, select Assign policy.
  2. 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
  3. On Parameters, set Tag Name to CostCenter.
  4. 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.
  5. 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.

  1. In Policy → Assignments, change the Scope filter to your lab resource group, rg-CLOUD-LAB-GRP-N.
  2. Select the assignment Lab cost guardrails. Select View definition.
  3. Look at the list of policies. Each has a code and a title, such as P01A-REGION or P03A-VM-SIZE. These are the codes in the [LAB-...] messages.
  4. Select one definition, for example P03B-VM-SPOT, and open its Definition tab. Find the effect, and the if condition 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

  1. Go to your lab resource group, not the sandbox, and select + Create.
  2. Search for Network security group and select it.
  3. Set the region to East US and select Review + create.
  4. 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.

  1. In Policy, select Compliance. Set the Scope to your sandbox.
  2. For each of your three assignments, find the Compliance state.
  3. Select audit-location-N. You should see nsg-N-sea3 as Non-compliant, because it is not in the region of the resource group. nsg-N-sea is compliant.
  4. Select inherit-costcenter-N. You should see both NSGs as Non-compliant: they don't have the CostCenter tag.

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.

  1. In Policy, select Remediation, then the Policies to remediate tab. Set the scope to your sandbox.
  2. Select inherit-costcenter-N, then Remediate.
  3. Leave the default settings and select Remediate.
  4. Open the Remediation tasks tab. Wait until the status is Complete. Refresh if needed.
  5. Open nsg-N-sea. Under Tags, you should see CostCenter = 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

  1. In your sandbox, select + Create and search for Virtual network.
  2. Name it vnet-N-test, set the region to East US, and select Review + create.
  3. The validation fails with RequestDisallowedByPolicy. Open the details. It names your assignment allowed-locations-N.
  4. 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

  1. In Policy → Assignments, set the scope to your sandbox.
  2. For each of your three assignments, select … → Delete assignment.
  3. 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-sea3 as non-compliant for your Audit policy.
  • The remediation task added the CostCenter tag 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.