Skip to content

Azure landing-zone hierarchy and policy lab

Validates pinned-provider Terraform hierarchy, policy, identity and logging assumptions without deploying cloud resources.

Cloud Security 5 min read

Implementation

main.tf Terraform · 165 lines
labs/azure-landing-zone/terraform/main.tf
1terraform {
2  required_version = ">= 1.7.0, < 2.0.0"
3  required_providers {
4    azurerm = {
5      source  = "hashicorp/azurerm"
6      version = "4.50.0"
7    }
8  }
9}
10 
11provider "azurerm" {
12  features {}
13  subscription_id                 = var.subscription_id
14  tenant_id                       = var.tenant_id
15  use_oidc                        = true
16  use_cli                         = false
17  resource_provider_registrations = "none"
18}
19 
20variable "tenant_id" {
21  type = string
22  validation {
23    condition     = can(regex("^[0-9a-f-]{36}$", var.tenant_id))
24    error_message = "Use a dedicated lab tenant UUID."
25  }
26}
27 
28variable "subscription_id" {
29  type = string
30  validation {
31    condition     = can(regex("^[0-9a-f-]{36}$", var.subscription_id))
32    error_message = "Use a dedicated lab subscription UUID."
33  }
34}
35 
36variable "parent_management_group_id" {
37  type        = string
38  description = "Existing isolated parent; never place the lab at the tenant root."
39  validation {
40    condition     = can(regex("^/providers/Microsoft.Management/managementGroups/[a-zA-Z0-9-]+$", var.parent_management_group_id))
41    error_message = "Use the resource ID of the isolated existing parent."
42  }
43}
44 
45variable "prefix" {
46  type    = string
47  default = "research-lz"
48  validation {
49    condition     = can(regex("^[a-z][a-z0-9-]{2,19}$", var.prefix))
50    error_message = "Use a short lab-only resource prefix."
51  }
52}
53 
54variable "subject" {
55  type        = string
56  description = "Exact GitHub environment subject actually observed; no default."
57  validation {
58    condition     = can(regex("^repo:[^*?<> ]+/[^*?<> ]+:environment:lab$", var.subject))
59    error_message = "Use an exact repository subject bound to the protected lab environment."
60  }
61}
62 
63variable "policy_effect" {
64  type    = string
65  default = "audit"
66  validation {
67    condition     = contains(["audit", "deny"], var.policy_effect)
68    error_message = "Only audit or deny; Disabled is deliberately unavailable."
69  }
70}
71 
72resource "azurerm_management_group" "root" {
73  name                       = var.prefix
74  display_name               = "Isolated research landing-zone root"
75  parent_management_group_id = var.parent_management_group_id
76}
77 
78resource "azurerm_management_group" "children" {
79  for_each                   = toset(["platform", "landing-zones", "sandbox"])
80  name                       = "${var.prefix}-${each.key}"
81  display_name               = each.key
82  parent_management_group_id = azurerm_management_group.root.id
83}
84 
85resource "azurerm_policy_definition" "locations" {
86  name                = "${var.prefix}-locations"
87  policy_type         = "Custom"
88  mode                = "Indexed"
89  display_name        = "Lab location guardrail"
90  management_group_id = azurerm_management_group.root.id
91  policy_rule = jsonencode({
92    if = { allOf = [
93      { field = "location", exists = "true" },
94      { field = "location", notIn = ["canadacentral", "canadaeast", "global"] }
95    ] }
96    then = { effect = var.policy_effect }
97  })
98}
99 
100resource "azurerm_management_group_policy_assignment" "locations" {
101  name                 = "${var.prefix}-locations"
102  display_name         = "Lab location guardrail — impact review required before deny"
103  policy_definition_id = azurerm_policy_definition.locations.id
104  management_group_id  = azurerm_management_group.root.id
105  enforce              = true
106}
107 
108# The subscription stays in its existing hierarchy. No subscription moves,
109# subscription vending, provider registration, networks or public endpoints.
110resource "azurerm_resource_group" "logs" {
111  name     = "${var.prefix}-logs"
112  location = "canadacentral"
113}
114 
115resource "azurerm_log_analytics_workspace" "lab" {
116  name                = "${var.prefix}-workspace"
117  resource_group_name = azurerm_resource_group.logs.name
118  location            = azurerm_resource_group.logs.location
119  sku                 = "PerGB2018"
120  retention_in_days   = 30
121  daily_quota_gb      = 1
122}
123 
124resource "azurerm_monitor_diagnostic_setting" "activity" {
125  name                       = "${var.prefix}-activity"
126  target_resource_id         = "/subscriptions/${var.subscription_id}"
127  log_analytics_workspace_id = azurerm_log_analytics_workspace.lab.id
128  dynamic "enabled_log" {
129    for_each = toset(["Administrative", "Policy", "Security"])
130    content {
131      category = enabled_log.value
132    }
133  }
134}
135 
136resource "azurerm_user_assigned_identity" "reader" {
137  name                = "${var.prefix}-evidence-reader"
138  resource_group_name = azurerm_resource_group.logs.name
139  location            = azurerm_resource_group.logs.location
140}
141 
142resource "azurerm_federated_identity_credential" "github" {
143  name                = "lab-evidence-review"
144  resource_group_name = azurerm_resource_group.logs.name
145  parent_id           = azurerm_user_assigned_identity.reader.id
146  audience            = ["api://AzureADTokenExchange"]
147  issuer              = "https://token.actions.githubusercontent.com"
148  subject             = var.subject
149}
150 
151resource "azurerm_role_assignment" "read_evidence" {
152  scope                = azurerm_log_analytics_workspace.lab.id
153  role_definition_name = "Log Analytics Reader"
154  principal_id         = azurerm_user_assigned_identity.reader.principal_id
155}
156 
157output "review_contract" {
158  value = {
159    root              = azurerm_management_group.root.id
160    policy_effect     = var.policy_effect
161    log_categories    = ["Administrative", "Policy", "Security"]
162    subscription_move = "not configured; assignment inheritance for this subscription is not established"
163    identity_scope    = "workspace read-only; bootstrap authority is external"
164  }
165}

Run it

  • bash labs/azure-landing-zone/validate.sh

Terraform objective and evidence boundary

Partially tested: the pinned AzureRM 4.50.0 provider validates the Terraform schema and three mock-plan tests. No Azure token exchange, policy evaluation, resource creation, subscription move or log delivery was executed. The older Bicep example below is retained as an alternative compilation exercise.

Threat model and architecture

A workload contributor must not change inherited policy or audit ownership. Assets are hierarchy, policy, evidence identity and log coverage. Entry points are parent moves, policy effects, federated subject and diagnostic configuration. Bootstrap administration is trusted separately from a read-only evidence identity.

flowchart TD
  P[Existing isolated parent management group] --> R[Lab root]
  R --> F[Platform]
  R --> W[Landing zones]
  R --> S[Sandbox]
  R --> A[Audit-first location policy]
  U[Existing disposable subscription, not moved] --> D[Activity diagnostics]
  D --> L[Lab Log Analytics workspace]
  G[Exact GitHub lab subject] --> I[Read-only evidence identity]
  I --> L

Prerequisites and complete local validation — tested

Terraform >=1.7,<2, internet to initialize AzureRM 4.50.0, and about 1 GiB disk for provider code. No Azure login or credential is needed for mocked tests.

bash labs/azure-landing-zone/validate.sh

Expected: schema validation succeeds and three runs pass: audit-first/scoped identity, Disabled-policy rejection and wildcard-subject rejection. Mock plans use all-zero synthetic UUIDs and never call Azure. Keep the generated provider lockfile in version control. Source is terraform/main.tf.

Management-group, identity, logging and guardrail assumptions

  • The existing parent is an isolated lab parent in a dedicated tenant. A bootstrap identity would need narrowly reviewed hierarchy/policy rights there and resource, identity, diagnostic and role-assignment rights in the disposable subscription. This module deliberately does not grant its own bootstrap permissions.
  • The subscription is not moved beneath the new hierarchy. Runtime policy inheritance for that subscription is not established. Never infer inherited enforcement from a hierarchy diagram or mock plan.
  • Default policy effect is audit; it records noncompliance and does not block it. deny is an explicit input for a later impact-reviewed experiment. Disabled is rejected. Tenant-specific aliases and actual policy evaluation remain untested.
  • The evidence identity receives Log Analytics Reader at the workspace only. Its exact issuer/audience/subject trust and GitHub environment must be separately checked with an actual non-production token exchange before use.
  • Subscription activity categories Administrative, Policy and Security are sent to a lab workspace in the design. Data-plane services, Entra logs, independent log-archive protection and source freshness are outside it. The workspace is in the lab subscription and is not an independent production archive.
  • Retention is 30 days and daily quota is 1 GiB. A quota is not a guaranteed bill cap and can cause monitoring gaps. No public service/network resource is created by the template, but workspace private connectivity is not configured here.

Evidence, cost and cleanup

mkdir -p .artifacts/research
bash labs/azure-landing-zone/validate.sh > .artifacts/research/azure-terraform.txt 2>&1

No cloud resources exist to destroy. Remove local .terraform/ downloads and retained evidence after review, keeping .terraform.lock.hcl. A later authorized cloud run would incur workspace ingestion/retention and possibly related service charges; costs and policy/logging effectiveness are not measured. It requires separate state, plan review and a reviewed destroy plan that first removes only lab policy/diagnostic/role assignments, then lab identities/workspace/groups, then empty child and root management groups. Never delete the existing parent, move subscriptions, or weaken production inheritance as cleanup.

Read the maintained landing-zone research and lab audit. The Azure experiment does not establish AWS Organizations/SCP behavior.

Historical alternative: Bicep compilation

This lab compiles a small tenant-scope Bicep design for a management-group hierarchy and an audit-first custom policy assignment. It demonstrates deployment shape, federated CI identity, and a reviewable what-if boundary; it does not deploy during repository validation.

Architecture decision

The sample separates platform, landing-zone, and sandbox subscriptions beneath an organization root. The policy begins with audit, because changing a broad management-group policy to deny before impact analysis can interrupt workloads. Production design still needs connectivity, identity, security, decommissioned, and workload-archetype management groups based on the organization's operating model.

Prerequisites and compile

  • Azure CLI 2.83.0 or later with Bicep support.
  • No Azure login is needed to compile.
az bicep build --file labs/azure-landing-zone/main.bicep

Expected result: Bicep returns a compiled ARM template without diagnostics. The repository code validator performs this compilation when Azure CLI is available and reports an explicit limitation otherwise.

What-if, not deployment

After replacing every angle-bracket placeholder, an authorized reviewer may inspect:

az login --tenant <tenant-id> --allow-no-subscriptions
az deployment tenant what-if \
 --name landing-zone-review \
 --location canadacentral \
 --template-file labs/azure-landing-zone/main.bicep \
 --parameters '@labs/azure-landing-zone/parameters.example.json'

The included workflow uses GitHub OIDC and a protected environment. Configure the federated credential with an exact repository/environment subject, constrain the service principal to the required tenant/management-group operations, and require a human review of the what-if artifact. Do not add a client secret.

Deployment and rollback considerations

Management-group and policy changes have a wide blast radius. Roll out new policy in audit mode, inventory noncompliance, assign named exemption owners with expiry, pilot a narrow management group, then move to deny only with tested break-glass and rollback procedures. Removing a policy assignment does not undo resource mutations performed by deployIfNotExists or modify; this sample deliberately uses audit.

Negative cases to review

  • placeholder or duplicate management-group IDs;
  • an OIDC subject broader than the protected environment;
  • policy enforcement before exemptions and regional dependencies are inventoried;
  • a hierarchy move that changes inherited role and policy scope;
  • subscription vending without ownership, budget, network, logging, and lifecycle metadata;
  • a tenant-level deployment identity with permanent credentials or excessive roles.

Cleanup and limitations

Compilation creates main.json; remove that generated file after inspection. This lab does not execute what-if in CI because it has no authorized tenant, and it does not deploy, move subscriptions, create role assignments, configure networking, or validate tenant-specific Azure Policy aliases. What-if itself is predictive output, not proof of runtime correctness.

References