Lesson 1 of 4 Governance with Azure Policy and Infrastructure as Code

Understanding Azure Policy

This lesson explains definitions, initiatives, assignments and effects, and how policy differs from RBAC.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish Azure Policy from RBAC.
  • Describe definitions, initiatives and assignments.
  • Choose a policy effect.
  • Explain how existing resources are remediated.

Why policy is separate from access control

Azure RBAC answers the question of what a person may do. Azure Policy answers a different question: what properties must a resource have? A person with Contributor rights can create a storage account, and RBAC has no opinion about whether that account allows public access. Policy does. The two work together, and neither replaces the other. A useful test when reading a scenario is to ask whether the requirement concerns who acts, which points to RBAC, or what may exist, which points to policy.

Policy evaluates resources when they are created or changed, and it also scans existing resources periodically, about every 24 hours. It can therefore both stop a new problem and report an old one.

Sort into groups

RBAC or Azure Policy?

Exam skills 1.3.1 · 1.3.5

Decide whether each requirement concerns who acts or what may exist.

Log in to try this activity Interactive activities are free with an account.

Definitions, initiatives and assignments

Three objects make up the system.

  • A policy definition holds the rule. It says which resources to evaluate and what effect to apply. Azure supplies many built-in definitions.
  • An initiative, also called a policy set, groups related definitions so that they can be assigned and tracked together. The Microsoft Cloud Security Benchmark is an initiative.
  • An assignment applies a definition or initiative to a scope, with parameters and optional exclusions.

An assignment at a management group flows down to everything beneath it, which makes policy the natural tool for organization-wide rules. A child scope cannot weaken it, although an exclusion on the assignment can leave out a scope, and an exemption can record a justified exception for a resource, with an expiry date.

The following diagram shows how definitions, initiatives and assignments relate.

The objects of Azure Policy. A policy definition holds a rule and its effect. An initiative groups related definitions, such as the Microsoft Cloud Security Benchmark. An assignment applies a definition or initiative to a scope with parameters, and flows down to everything beneath it. An exclusion on the assignment leaves out a scope, and an exemption records a justified exception for one resource, with an expiry date.
A child scope cannot weaken an inherited assignment.

Policy effects

The effect decides what happens when a resource matches. The following table lists the effects that security engineers use most, and they are worth learning by what each one does at the moment of the request and afterwards.

EffectOn a new or changed resourceOn an existing resourceNeeds a managed identity
DenyRejects the request with a 403 errorReports it as noncompliantNo
AuditAllows it, and records a warningReports it as noncompliantNo
ModifyAdds or changes properties or tags during the requestFixed by a remediation taskYes
AppendAdds fields to the requestReports it as noncompliantNo
AuditIfNotExistsChecks for a related resource, and reports if it is absentReports it as noncompliantNo
DeployIfNotExistsDeploys a related resource if it is absentFixed by a remediation taskYes
DisabledDoes nothingDoes nothingNo

A common approach is to assign a new policy with Audit first, examine the compliance results, and then move to Deny. Policies with Deny can break deployments, so a sudden change may stop legitimate work. Many built-in definitions take the effect as a parameter, so the same definition can move from one to the other. The DoNotEnforce enforcement mode also lets you see the results of an assignment without applying the effect.

The following diagram shows which effects act during a request and which act on existing resources.

When policy effects act. During a create or update request, Deny rejects it with a 403 error, Modify and Append change the request, and Audit allows it with a warning. After the resource exists, compliance evaluation reports noncompliant resources; AuditIfNotExists reports a missing related resource, and Modify and DeployIfNotExists fix existing resources through a remediation task that uses a managed identity.
Start with Audit, review the results, then move to Deny.
Match the pairs

Policy effects

Exam skills 1.3.1

Match each policy effect with what it does to a new or changed resource.

Log in to try this activity Interactive activities are free with an account.

Remediating existing resources

A new assignment does not change anything that already exists. For Modify and DeployIfNotExists policies, a remediation task corrects existing resources. The assignment uses a managed identity, which needs roles that are sufficient for the change, because the identity rather than the original deployer makes the correction.

A policy that deploys diagnostic settings to all key vaults shows the pattern. New vaults receive the setting automatically through the policy. Existing vaults are marked noncompliant until a remediation task is run. If that task fails, the likely cause is a role missing from the identity, or a network rule that blocks the deployment.

Evaluation order and interaction

If several assignments apply, all are evaluated, and the outcome is the most restrictive one. A Deny from any assignment stops the request, so policies do not need a priority. Policy is evaluated by Azure Resource Manager, after the RBAC check. A user without the right role is rejected by RBAC first, and a user with the right role can still be rejected by policy.

Because policy works on Resource Manager properties, it cannot see inside a virtual machine. Settings in the guest operating system need Azure Machine Configuration, which is delivered through policy and is described in a later module.

True or false

How policies combine

Exam skills 1.3.1

Check how Azure Policy evaluates requests and assignments.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing an effect

A company requires that all storage accounts have public blob access disabled. New accounts that violate this must be stopped. The company has 300 existing accounts, and it must see which ones violate the rule before changing them. What should you do?

The built-in definition for public blob access has a parameterized effect. Assign it first with Audit at a management group, and review the compliance page to identify the existing accounts. Work with the owners, and then change the effect to Deny so that new violations are stopped.

Starting with Deny would stop new accounts, but it might block a legitimate update to an existing account that is still noncompliant. A remediation task is not available for a Deny or Audit effect, because it applies only to Modify and DeployIfNotExists. An RBAC role cannot express the rule, since it concerns a property rather than an action.

Check yourself

Which identity makes the change during a DeployIfNotExists remediation?

Show answer

The assignment's managed identity, which must hold roles sufficient for the change.

Exam Tips

Remember for the exam

  • Policy governs what exists; RBAC governs who acts.
  • Audit first, then Deny.
  • Modify and DeployIfNotExists need a managed identity and a remediation task for existing resources.
  • A Deny from any assignment stops the request.