Home› Blog› Week 1 — The Policy Was Perfect and It Governed No…
Azure Weekly Lab Azure Terraform

Week 1 — The Policy Was Perfect and It Governed Nothing

A deny rule that was correctly written, correctly assigned, and enforcing — while deployments it should have blocked sailed straight through. The assignment was not the problem.

Verified against current vendor documentation on 22 August 2026. Pricing, limits and API behaviour were checked against the official docs on that date. Cloud services change fast — if you are reading this much later, treat the specifics as a starting point and re-check the linked sources.
Management Groups Azure Policy Subscription Alias API Entra Workload Identity Terraform HCP Terraform
Azure Platform Engineering Lab · Week 1 of 52

Lab Overview

Azure gives you three nested scopes: management groups, subscriptions and resource groups. Almost every landing zone that goes wrong goes wrong because somebody picked one of them as the real boundary and treated the other two as packaging.

They are not three sizes of the same box. They answer three different questions, and the answers are not interchangeable.

What is allowed here?

Management group. Policy and RBAC inherit downward, to subscriptions that do not exist yet. Holds no resources — nothing deploys into one.

What is the ceiling?

Subscription. Quota is counted per subscription per region. Also the billing boundary and the resource-provider registration boundary.

And the third: a resource group answers what dies together. It is the only one of the three whose deletion is atomic and complete.

That last fact settles a question people argue about for weeks. If you want a disposable unit — one per experiment, one per week, one per feature branch — it has to be a resource group, because cancelling a subscription is not a teardown. Billing stops immediately and services are disabled, but the subscription itself persists: the portal's Delete subscription option does not appear until three days after cancellation, Azure deletes it automatically only at 90 days, and it can be reactivated throughout. A resource group deletes now.

The design falls out of the three answers. Disposable unit → resource group. Quota and blast-radius separation → subscription. Policy posture → management group. Nothing about that requires taste; it requires reading what each scope actually does.

DifficultyIntermediate — seven resources, but policy scope and evaluation timing are where the surprises are
Time to completeAbout 90 minutes, most of it waiting for compliance to evaluate
DeploymentTerraform (azurerm), state in HCP Terraform, local execution
Measured cost$0.00 — management groups, policy and an empty VNet are all free
Full lab guideLAB.md — prerequisites, variables, commands, expected outputs, validation and cleanup

Cloud Services and Concepts

ServicePurpose hereWhat it is
Management groups The folders the rules live in A container for subscriptions, and the scope where governance is set. Anything applied here is inherited by everything beneath it, so a rule attaches to a position in the tree rather than to a named subscription.
Azure Policy Checks every resource against a rule Evaluates resources and can refuse a deployment outright. A definition is one rule, an initiative bundles several, an assignment points one at a scope.
Policy initiative Carries the three baseline rules as one object Allowed locations, a required tag and the custom no-public-IP rule, so a scope receives all three from a single assignment.
Policy aliases What a custom rule is allowed to read The property paths Azure exposes to Policy. A rule cannot read arbitrary JSON, only these paths, and the [*] wildcard changes a test from “the resource” to “every element in this array”.
Subscriptions What pays, and what inherits The billing and quota boundary. Four sit under this tree and are governed by where they sit, never by being named in an assignment.

Architecture and Workflow

Architecture diagram. A custom policy definition, deny-public-ip-on-nic, is defined once at the mg-katta management group and bundled into an initiative with allowed locations and a required tag. That one initiative is assigned twice: at mg-platform, where it always enforces, and at mg-lz-dev, where it is staged. Neither assignment names a subscription, and neither sits at the Tenant Root Group. Four subscriptions inherit the rules by position in the tree rather than by being listed. A test bed resource group holding a virtual network and subnet sits under the dev landing zone and is governed by inheritance alone.
The whole week on one page. One definition, bundled once, assigned twice — and never at a subscription. Everything on the right inherits by where it sits in the tree.
Azure portal management groups blade showing four subscriptions nested across nine management groups
Four subscriptions in nine groups. Tenant and subscription IDs are masked by the capture tooling before the file is written — see Security.

Three decisions in that tree are worth defending.

Nothing is assigned at Tenant Root Group. An assignment there reaches every subscription in the tenant, forever, including any created later by anyone for any reason — and undoing it needs elevated access. The intermediate root is what keeps a mistake survivable.

Connectivity and Management are separate. Whoever can rewrite the hub network's routing should not, by the same grant, be able to read every diagnostic log in the estate or rotate an encryption key. Splitting them costs nothing, because management groups are free.

Decommissioned exists because cancellation is not deletion. A retired subscription needs somewhere to sit under a deny-all assignment for its retention window. That node is not tidiness; it is a direct consequence of how Azure behaves.

Prerequisites

  • Permission to create management groups in the tenant. By default only the Global Administrator can, and it has to be elevated once at the tenant root before Azure RBAC applies there at all. This is the step that stops most people, and it is not a subscription permission.
  • Resource Policy Contributor or Owner at the management groups the assignments target — again, not on the subscription beneath them.
  • Contributor on a subscription for the test bed, which is where the deny attempts are made.
  • Terraform ≥ 1.10 with azurerm ~> 5.2.
  • Four resource providers: Microsoft.PolicyInsights, Microsoft.Resources, Microsoft.Network and Microsoft.Storage.
  • Patience, and a way to force a scan. A deny is instant, because policy evaluates on write. The compliance scan of resources that already exist is scheduled — roughly 15 minutes after a resource changes, 30 after a subscription moves, and every 24 hours otherwise. An empty compliance report means "not scanned yet", not "compliant".
  • No prior week is required. This is the first one.

Every variable and the enforcement switch are in the lab guide, which is the reproducible half of this write-up.

Implementation

0. How any of this gets deployed

Everything below is Terraform. There is no portal click anywhere in this week — the screenshots are the result of an apply, not a record of someone configuring things by hand.

State lives in HCP Terraform; the apply runs locally:

In versions.tf.

The workspace is set to local execution deliberately. HCP defaults a new workspace to remote, which fails immediately here because there are no Azure credentials stored in the workspace — the apply authenticates with whatever az login session is on the machine. So HCP holds the state file and its version history, and the plan and apply run locally. There are no HCP run logs for this week, because there were no HCP runs.

Bootstrap creates the tree, the CI identity and the subscriptions. Everything after that is one workspace per week.

HCP Terraform holds the state, not the runs — yet. Both workspaces store state remotely, so nothing is checked in and nothing sits on a laptop. Execution mode is still local: every plan and apply behind this post ran on my own machine against my own credentials. That means no run history, no approval gate, and no audit trail beyond a terminal. Remote execution needs the federated credentials wired into each workspace as environment variables, which is a later week — until then, "it is in HCP" is a claim about state and nothing more.

The custom rule denies a public IP on a network interface. The alias is the part that has to be right:

The policy rule
Microsoft.Network/networkInterfaces/ipConfigurations[*].publicIpAddress.id

The [*] matters. A network interface can carry several IP configurations. Without the wildcard the rule inspects only the first, and happily admits an interface whose second configuration is public. With it, the condition is true if any configuration has one.

The effect is a parameter, not a constant, which is what lets the same definition be assigned twice — reporting first, enforcing second — without editing it.

Two stages, on purpose
./scripts/deploy.sh report     # enforce = false → evaluates, blocks nothing
./scripts/validate.sh          # all three attempts succeed; two are non-compliant

./scripts/deploy.sh enforce    # enforce = true  → denies
./scripts/validate.sh          # two rejected, the compliant one still deploys

Introducing a deny to an environment nobody has measured is how a guardrail causes the outage it exists to prevent. Report-only mode tells you what would have been blocked before anything is.

Azure Policy assignments blade listing two Lab baseline initiative assignments scoped to Landing Zones - Dev and Platform
Both assignments are initiatives at management group scope. Neither one names a subscription anywhere — that is what makes them apply to subscriptions created later.

The third test is not padding. A rule that only ever blocks has been shown to be on, not to be correct. Proving the compliant path still deploys is what separates a guardrail from an outage.

Validation and Evidence

A successful apply proves an assignment exists. It does not prove the assignment refuses anything — so the validation attempts three real creations against the live policy, stating its expectation before each one.

AttemptExpectedActualStatus
A NIC carrying a public IPdenieddenied by policyPASS
A storage account in westeurope, not an allowed locationdenieddenied by policyPASS
A NIC with no public IP, in an allowed regionallowedcreatedPASS

The third attempt is the one most labs skip, and it is what makes the first two mean anything. A policy that denied everything would pass both deny tests and be useless.

What compliance actually reported

Azure Policy compliance blade showing the dev landing zones assignment non-compliant at 15 percent with 17 non-compliant resources
After a forced scan: 15% compliant, 17 non-compliant resources across 3 policies. The platform assignment above it reads 100% (0 out of 0) — nothing has been deployed into that scope yet.

Both rows are worth reading carefully, because they say opposite things and look similar.

The dev row is real: 15% (3 of 20), seventeen violations. Every one of those resources was created while the assignment was in report-only mode, and switching to enforcing removed none of them.

The platform row is 100% (0 out of 0) — an empty scope, not a clean one. Same green tick, entirely different meaning.

Security and Cost

Security

No client secrets. CI authenticates through federated credentials on an Entra app registration — HCP Terraform presents an OIDC token, Entra exchanges it because issuer, audience and subject match. There is no azuread_application_password anywhere in the repository.

Not Owner. The CI identity holds Contributor, Role Based Access Control Administrator and Management Group Contributor, scoped to the intermediate root. Owner bundles "can deploy" and "can grant roles" with no way to separate them; split, the grant-roles half is a visible line in the audit trail.

Nine identities, seven revoked. Auditing access by listing app registrations found five. Auditing from the assignment side — every role assignment, resolved back to its principal — found nine, because registrations created by portal wizards and shell sessions have no owner and never appear in an owner-filtered list. Seven held standing Contributor or Owner on the subscription. All seven lost it.

Screenshots that refuse to leak. Captures run through Playwright against a signed-in profile. The tooling masks the tenant ID, subscription IDs, billing identifiers and object IDs across text nodes, attributes and form-control values, then asserts they are absent and exits non-zero rather than write the file. It also refuses to save a sign-in page — checked by URL first, then DOM, and re-checked immediately before the shutter.

Cost

$0. Management groups are free. Subscriptions created through the alias API are free — a subscription is a billing container, and an empty one bills nothing. App registrations, federated credentials, policy definitions, assignments and compliance evaluation are all free.

The test bed was a virtual network, a subnet, two network interfaces and a handful of storage accounts, all created and destroyed within the session — under a penny in total.

Budgets exist on all three subscriptions before anything with an hourly price lands in them, alerting at 50% actual and 90% forecast. Forecast, not actual: an actual-spend alert at 90% of a monthly budget arrives after the money is already committed.

Troubleshooting and Lessons

The deny that did not deny.

  • Problem: enforcing mode was on. The assignment reported enforcementMode: Default, the effect read Deny, the allowed-locations list was right — and a storage account in a banned region deployed anyway.
  • Root cause: the subscription was not underneath the assignment. Created but never placed, it still sat at the Tenant Root Group on a different branch entirely. Policy flows downward to whatever is beneath it, and nothing was.
  • Resolution: place the subscription. Creating one and placing it are two separate operations — the Subscription Alias API creates it at the tenant root, and moving it is a second call. An unplaced subscription inherits no policy and no role assignments, and looks completely healthy right up until something is supposed to stop you and does not.
  • Lesson learned: check placement first. Every field can be right while the policy governs an empty branch. The tell was in the portal all along — the management group list showed 0 subscriptions against the target. I was reading the assignment instead, because an assignment is easy to inspect and looks convincing.

Thirty-four minutes for one subscription.

  • Problem: three subscriptions requested concurrently produced an apply that sat for an hour and committed nothing to state.
  • Root cause: subscription creation is slow and does not parallelise. Requested one at a time, the first took about ten minutes and the second failed outright with AliasCreate: StatusCode=0 — context deadline exceeded.
  • Resolution: request them serially and expect tens of minutes.
  • Lesson learned: an apply that commits nothing after an hour is worse than one that fails fast.

"100% (0 out of 0)" is not a pass.

  • Problem: the platform assignment reported perfect compliance.
  • Root cause: nothing had been deployed into that scope. An empty scope and a clean one render identically.
  • Resolution: read the denominator, not the percentage.
  • Lesson learned: the same green tick means "governed and compliant" in one row and "governing nothing" in the next.

The rejection that explained the wrong thing.

  • Problem: a denial names the initiative, not the rule inside it that actually refused.
  • Root cause: RequestDisallowedByPolicy reports the assignment, and an assignment can carry many rules.
  • Resolution: read the policy definition ID in the error body rather than the assignment name in the summary.
  • Lesson learned: the summary line sends you to the wrong file.

Cleanup and Key Takeaways

Cleanup

This is the week where "delete the resource group" is not the whole answer, and that is the part worth carrying forward.

The definition, the initiative and both assignments live at management group scope — outside every resource group, in a part of the hierarchy that resource-group deletion cannot reach. Delete the group by hand and the guardrails remain, still evaluating, still denying, with nothing left in the subscription to explain where they came from.

An orphaned deny assignment whose source nobody can find is a genuinely nasty thing to inherit. terraform destroy removes both because Terraform tracks both, and the cleanup script verifies afterwards that the assignment is actually gone rather than assuming it.

References

Key takeaways

  • Verify placement before you verify configuration. An assignment is easy to read and looks convincing, and every field can be correct while it governs an empty branch. Ask what is underneath the scope first.
  • The only proof a deny works is a deployment that got denied. Reading the assignment verifies the wrong object. That is why this week ships a test script rather than a screenshot of a portal blade.
  • Enforcing does not clean up. Seventeen non-compliant resources created during report-only mode were still there afterwards. Policy evaluates on write; it blocks the next deployment and has no opinion about what already exists. Remediation is a different mechanism.
  • A green tick with a zero denominator means nothing was examined. Register Microsoft.PolicyInsights before you trust a compliance number, because enforcement works fine without it.
  • Targeted applies leave half-built objects. Creating a subscription with -target created the subscription and skipped its management group placement and its budget, because those are separate resources rather than attributes. It looked finished for two days.
  • The error message is the product. A guardrail whose rejection sends someone to debug the wrong subsystem has made the platform worse, not safer. Per-policy messages cost one line each.

Next week: policy as code — initiatives, exemptions, and deployIfNotExists with the remediation identity that everybody forgets to grant.

Comments

How was your experience?
Your feedback helps improve this site.
PoorExcellent