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
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}
1targetScope = 'tenant'
2
3@description('Stable management group identifier for the organization landing-zone root.')
4param rootManagementGroupId string
5
6@description('Display name for the organization landing-zone root.')
7param rootDisplayName string
8
9@description('Azure region required for tenant-scope deployment metadata.')
10param deploymentLocation string = 'canadacentral'
11
12@description('Locations audited by the sample baseline policy.')
13param allowedLocations array = [
14 'canadacentral'
15 'canadaeast'
16]
17
18resource root 'Microsoft.Management/managementGroups@2023-04-01' = {
19 name: rootManagementGroupId
20 properties: {
21 displayName: rootDisplayName
22 }
23}
24
25resource platform 'Microsoft.Management/managementGroups@2023-04-01' = {
26 name: '${rootManagementGroupId}-platform'
27 properties: {
28 displayName: 'Platform'
29 details: {
30 parent: {
31 id: root.id
32 }
33 }
34 }
35}
36
37resource landingZones 'Microsoft.Management/managementGroups@2023-04-01' = {
38 name: '${rootManagementGroupId}-landing-zones'
39 properties: {
40 displayName: 'Landing zones'
41 details: {
42 parent: {
43 id: root.id
44 }
45 }
46 }
47}
48
49resource sandbox 'Microsoft.Management/managementGroups@2023-04-01' = {
50 name: '${rootManagementGroupId}-sandbox'
51 properties: {
52 displayName: 'Sandbox'
53 details: {
54 parent: {
55 id: root.id
56 }
57 }
58 }
59}
60
61module baseline 'modules/policy-baseline.bicep' = {
62 name: 'landing-zone-audit-baseline'
63 scope: managementGroup(rootManagementGroupId)
64 params: {
65 allowedLocations: allowedLocations
66 assignmentLocation: deploymentLocation
67 }
68 dependsOn: [
69 root
70 ]
71}
72
73output managementGroupIds object = {
74 root: root.id
75 platform: platform.id
76 landingZones: landingZones.id
77 sandbox: sandbox.id
78}
1targetScope = 'managementGroup'
2
3param allowedLocations array
4param assignmentLocation string
5
6resource allowedLocationsDefinition 'Microsoft.Authorization/policyDefinitions@2023-04-01' = {
7 name: 'audit-allowed-locations'
8 properties: {
9 displayName: 'Audit resources outside reviewed locations'
10 description: 'Teaching policy: audit, rather than deny, while teams inventory exemptions and validate impact.'
11 policyType: 'Custom'
12 mode: 'Indexed'
13 metadata: {
14 category: 'Landing zone lab'
15 version: '1.0.0'
16 }
17 parameters: {
18 allowedLocations: {
19 type: 'Array'
20 metadata: {
21 displayName: 'Allowed locations'
22 }
23 }
24 }
25 policyRule: {
26 if: {
27 allOf: [
28 {
29 field: 'location'
30 exists: 'true'
31 }
32 {
33 field: 'location'
34 notIn: '[parameters(\'allowedLocations\')]'
35 }
36 ]
37 }
38 then: {
39 effect: 'audit'
40 }
41 }
42 }
43}
44
45resource allowedLocationsAssignment 'Microsoft.Authorization/policyAssignments@2024-04-01' = {
46 name: 'audit-allowed-locations'
47 location: assignmentLocation
48 properties: {
49 displayName: 'Audit resources outside reviewed locations'
50 description: 'Lab assignment begins in audit mode. Promote only after impact analysis, exemption ownership, and rollback validation.'
51 policyDefinitionId: allowedLocationsDefinition.id
52 enforcementMode: 'Default'
53 parameters: {
54 allowedLocations: {
55 value: allowedLocations
56 }
57 }
58 }
59}
60
61output policyDefinitionId string = allowedLocationsDefinition.id
62output policyAssignmentId string = allowedLocationsAssignment.id
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.denyis an explicit input for a later impact-reviewed experiment.Disabledis rejected. Tenant-specific aliases and actual policy evaluation remain untested. - The evidence identity receives
Log Analytics Readerat 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¶
- Azure landing zones
- Landing-zone design areas
- Management-group design guidance
- Subscription vending guidance
- AzureRM 4.50.0 diagnostic setting source documentation