Draw Tenant, Subscription and Resource Group Boundaries
Azure has four nested scopes, and each answers a different question. A tenant says who you are. A management group says which rules and role assignments you inherit. A subscription says where the work happens, what it costs and how much of it you may do. A resource group says what is deployed and destroyed together.
The tenant is the identity boundary. Users, groups, service principals, managed identities and app registrations all live in one directory, and that directory issues the tokens every other scope trusts. A subscription is associated with exactly one tenant at a time, so a provider instance is really a triple: a tenant, a subscription and a credential.
The subscription is the management boundary. Quotas, region availability, cost, policy scope and resource-provider registration attach to the subscription, not to a folder inside it. Moving a subscription between management groups changes what it inherits and nothing about its tenant; moving it between tenants is a transfer, with a new home directory and new role assignments.
The resource group is a lifecycle boundary, not an isolation boundary. Resources in one group can sit in different regions, and the group's own location only stores metadata. Deleting the group deletes everything inside it, including objects Terraform never created. RBAC and policy assigned at group scope are inherited downward, but they cannot subtract rights granted at subscription scope.
Management groups are where inherited control actually lives. A directory allows one hierarchy, each group has one parent, and the tree can run six levels below the tenant root. New subscriptions land under the root by default, so an unplanned subscription inherits whatever is assigned there rather than starting clean.
The isolation you want is a subscription, not a folder. Teams that separate production by resource group still share quotas, region availability and every subscription-scope role assignment. Use separate subscriptions per trust and billing boundary, management groups for inherited policy, and resource groups only for lifecycle.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','signalColor':'#5a6e82','noteBkgColor':'#c4873b','noteTextColor':'#132440','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
T["Microsoft Entra tenant
identities and token issuance"] --> R["Tenant root management group
default home of new subscriptions"]
R --> M["Management groups
policy and RBAC inheritance
six levels below root"]
M --> S1["Subscription: platform
quota, cost, region availability"]
M --> S2["Subscription: workloads
quota, cost, region availability"]
S1 --> G1["Resource group
deploy and destroy together"]
S1 --> G2["Resource group"]
G1 --> X1["Resources
one group each"]
G2 --> X2["Resources"]
T -.->|"service principals, groups,
managed identities"| S2
G2 -.->|"not an isolation boundary"| N["Same subscription quota,
policy scope and network boundary"]
G1 -.->|"one destroy"| D["Every object in the group
is deleted with it"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class T,R primary;
class M,S1,S2 secondary;
class G1,G2 ok;
class N warn;
class D danger;
class X1,X2 secondary;
Figure 1 draws containment downward and identity sideways. The dashed edges are the two that surprise people: the tenant issues the principals that act inside subscriptions, and a resource group looks like a security boundary without being one. Figure 2 turns the same stack into four questions.
The tenant root is the one scope nobody assigns by accident. A Global Administrator can elevate to User Access Administrator there, so treat the root management group as break-glass, not a landing zone. Any policy attached at the root applies to every subscription in the directory.
Choose CLI, Managed Identity or Federated Authentication
The provider does not invent an identity; it selects one from what the runner can offer. A laptop has an interactive session. Azure compute has a managed identity endpoint. An external pipeline has a signed OIDC token and nothing else. Pick the row that matches the runner, not the row that is easiest to paste.
The CLI path is the default, and that is the risk. use_cli is true unless something turns it off, and when the subscription is unset the provider adopts the CLI's current subscription. Two choices are then made silently: which account signed in, and which subscription is current.
Managed identity removes the secret, not the authorization problem. Set use_msi = true and the provider asks the local managed identity endpoint for a token; a user-assigned identity also needs its client ID. There is nothing to rotate and nothing to leak, but the identity still needs a role assignment at every scope it must touch.
Federated credentials are the right answer for external CI. The pipeline presents a short-lived OIDC token, Entra validates its issuer, audience and subject against a federated credential on an app registration, and returns an access token. The subject pins the repository, branch or environment, so a fork cannot mint the same token.
AKS workload identity is federation with a shorter walk. A user-assigned identity carries a federated credential, the pod projects a token into a file, and use_aks_workload_identity reads the three values from the environment. It is not the node's managed identity, and granting the node access to a Key Vault so that a pod can read a secret is the failure mode this replaces.
The provider can fetch that token for you. With use_oidc enabled, the provider accepts a token directly, reads one from a file, or requests one from the pipeline's own token endpoint. GitHub Actions and Azure DevOps expose that endpoint through environment variables the provider already understands.
The token file is read once. A rewrite during a long apply is invisible to the running provider, so treat the file as immutable for the life of the process.
A client secret is the one option that stores a bearer secret. Use it only where federation is impossible, keep the value in Key Vault, pass it through a file rather than a command line, and rotate on a schedule the pipeline can survive. A secret exported into a shell is inherited by every child process.
One environment authenticates all three providers. AzureRM, AzureAD and AzAPI read the same ARM_ variables, so a secret exported during a debugging session will silently satisfy a provider you believed was using federation. Declare credentials per provider block, and let a startup assertion prove which one was used.
Split the plan identity from the apply identity. A plan needs read access at the target scopes and nothing more; an apply needs write. Using one Contributor identity for both means every pull request runs with production write rights, and the review becomes advisory.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','signalColor':'#5a6e82','noteBkgColor':'#c4873b','noteTextColor':'#132440','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["terraform plan starts"] --> B{"Which credential is configured?"}
B -->|"secret or certificate"| C["Client credential flow
token for ARM and Graph"]
B -->|"use_msi = true"| D["Managed identity endpoint
no secret in configuration"]
B -->|"use_oidc = true"| E["Pipeline OIDC token
exchanged at the token endpoint"]
B -.->|"nothing set, so use_cli defaults true"| F["Azure CLI session
signed-in user and default subscription"]
C --> G{"Token tenant and audience valid?"}
D --> G
E --> G
F --> G
G -->|no| X1["AADSTS error
wrong tenant, audience or issuer"]
G -->|yes| H["Read the selected subscription"]
H -->|"403 AuthorizationFailed"| X2["Plan stops
no role at this scope"]
H -->|"200 OK"| I["Provider configures
and the plan proceeds"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class A primary;
class B,G,H secondary;
class C,D,E,I ok;
class F warn;
class X1,X2 danger;
A valid token is not a permission. Figure 3 ends at the subscription read because that is where identity stops and authorization begins: the same token that lists a subscription it owns returns AuthorizationFailed on one it does not.
Make Subscription Selection and Provider Registration Explicit
An omitted subscription is a decision made by the shell. The provider takes the value from its own argument, from ARM_SUBSCRIPTION_ID, or from the Azure CLI's current subscription, in that order of confidence. Only the argument appears in review, so only the argument is a decision.
Pin all three providers in one block. Terraform 1.17.1 with AzureRM 5.13.0, AzureAD 3.10.0 and AzAPI 2.13.0 inside one required_providers block, each with an exact version rather than a range. A range lets the Registry choose the binary that reads your state.
The features block is required, and its defaults are real decisions. An empty block is valid, but it accepts the 5.x defaults: a Terraform destroy of a resource group fails while it still contains resources, and a subscription destroyed through Terraform is not protected from cancellation. Both defaults are defensible; neither is neutral.
- Target:
subscription_idnames the subscription the diff will change. - Directory:
tenant_idnames the directory that must issue the token. - Instance:
aliasnames the provider, so two subscriptions never share one. - Registration:
resource_provider_registrationsdecides which APIs may be switched on. - Cross-tenant:
auxiliary_tenant_idscovers up to three additional tenants for genuinely cross-tenant resources.
The subscription is required to plan, not to validate. terraform validate passes without it, so a pipeline that stops at validation has not proved the provider can reach a subscription at all. The failure appears at the first plan, which is later than the configuration review.
Registration became explicit in 5.0. resource_provider_registrations defaults to none, so the provider no longer switches on APIs behind your back. The five sets are core, extended, all, legacy and none. The sets are frozen, and the documented direction is to name the providers you actually use.
Name providers precisely. resource_providers_to_register takes exact namespaces, such as Microsoft.ContainerService or Microsoft.KeyVault, and combines with a set. Two namespaces are a reviewable line; a set is a behaviour you inherit.
Registration is per subscription and needs a subscription-scope permission. Contributor and Owner carry it; a custom role that can create resources without the register action produces an authorization failure that reads like a quota problem. On a fresh subscription the first apply is slower because registration is asynchronous.
Do not mix the old switch with the new one. skip_provider_registration dates from 4.x, where it suppressed automatic registration. In 5.x the registration properties are the supported control, and leaving the legacy argument in place hides which behaviour is actually in force.
Move validation left where the block supports it. The enhanced_validation block can check locations and run preflight payload validation at plan time, turning a class of apply-time rejections into a failed plan. Both are best-effort, so neither replaces a real apply in a sandbox.
Aliases carry all of this per subscription. Name them for the tenant and subscription role, pass them into modules through providers, and never let a child module declare its own provider block.
Use AzureAD and AzAPI as Separate Companion Providers
Three providers, three APIs, three permission models. AzureRM talks to the ARM control plane. AzureAD talks to Microsoft Graph. AzAPI talks to ARM directly, at whatever api-version you name. They share credentials far more easily than they share ownership.
AzureAD owns the directory. Applications, service principals, groups, federated credentials and Graph app-role assignments are directory objects, and changing them needs Graph permissions, often with tenant-wide admin consent. In 3.x, azuread_application_registration is the resource that owns an application object; mixing it with the older application resource gives two owners of one registration.
AzAPI owns what AzureRM does not model. A resource declares a type with an api-version, a parent_id, a name and a body that maps to the REST payload. That is the right tool for a preview service or a property that has not reached the typed resource yet, and the wrong tool for anything AzureRM already manages well.
One object, one writer. When an azurerm_ resource and an azapi_resource both manage the same ARM object, each apply writes its own view of the payload and the other side reports drift on the next plan. The plan never converges, because both are correct and neither is authoritative.
Identities are the classic double-ownership trap. azurerm_user_assigned_identity creates an ARM identity that also appears in the directory; adding a service principal for the same client ID attempts to manage that directory object from a second place. Manage the identity with one resource and reference its IDs everywhere else.
Two role assignments, two planes. azurerm_role_assignment grants ARM RBAC on a management group, subscription, resource group or resource. azuread_app_role_assignment grants a Graph application permission to a principal. A workload can need both, and neither implies the other.
Sharing a state file is fine; sharing an object is not. All three providers can live in one root module and one state, and the dependency graph will order them correctly. What it cannot fix is two resources writing the same remote property, because the graph models dependencies, not exclusive ownership.
| Provider | Talks to | Owns in this stack | Must not own |
|---|---|---|---|
hashicorp/azurerm | ARM control plane at subscription scope | resource groups, networks, compute, ARM role assignments, managed identities | directory objects, preview APIs it does not model |
hashicorp/azuread | Microsoft Graph at tenant scope | app registrations, service principals, groups, federated credentials | any ARM resource or subscription-scope role assignment |
azure/azapi | ARM REST at a pinned api-version | preview resources and properties AzureRM has not surfaced | objects already owned by an azurerm_ resource |
The table is an ownership contract, not a feature list. A provider in two rows for one object starts a revert loop that stays invisible until two applies land in the same hour.
Escalate by pipeline, not by convenience. When the deployment identity cannot create a directory object, the answer is a short-lived, separately approved Graph pipeline, not permanent directory rights on the identity that runs every apply.
Prove the Selected Subscription Before Provisioning
AzureRM has no account guard, so the configuration must be one. The AWS provider can refuse to configure under an unexpected account; the Azure provider has no equivalent. What it does have is azurerm_client_config, which reports the tenant, subscription, client and object the provider actually resolved.
Read the identity the provider resolved, not the one you intended. Every alias needs its own client-config read, because a single check on the default instance proves nothing about the alias a module received. Compare tenant and subscription as values, not as strings copied into the same file.
Choose the identifier that matches the question. client_id names the application and survives a service principal being recreated. object_id names the principal in this tenant and changes when it is recreated. For a managed identity the two differ, and comparing the wrong one produces a check that passes for the wrong reason.
Put the assertion in a precondition. A check block reports a warning and still exits zero, which is fine for advisory hygiene and useless as a gate. A precondition on the resource fails the plan with your own message before anything is created.
Then prove one read. A resolved identity says nothing about authorization: the token can be valid and still have no role at the target scope. Read the resource group through the same alias, so an unprivileged identity fails with an authorization error before the first create.
Make the evidence a plan output. Tenant, subscription suffix, credential kind, issuer and subject for federation, and the provider versions. The full subscription identifier and every token stay out of published artefacts.
Do not mark the proof sensitive. A sensitive = true output disappears from the plan text and from CI logs, which is exactly where the reviewer needs it. Keep the record non-sensitive, and keep the subscription identifier out of anything published.
Prove the Selected Subscription Before Provisioning
Status: design-only. No Azure subscription, credential or billable resource was used here, and none of these commands were executed. Every identifier is a placeholder: the sandbox is the subscription you nominate, and the second is an untouched one you may name but must not write to.
Prerequisites. Terraform CLI 1.17.1, the three providers at 5.13.0, 3.10.0 and 2.13.0, Azure CLI signed in to the sandbox tenant, and permission to read both the subscription and the sandbox resource group. Cost is zero: client-config reads, a resource group read and the built-in test resource create nothing billable.
Setup. Four files in one directory, no modules. The variables hold the subscription that feeds the provider, the subscription and tenant you expect to see, and a resource group that exists only in the sandbox.
Two gates, tested separately. They fail at different moments, which is why the lab tests both.
| Gate | What it proves | Failure looks like |
|---|---|---|
| Identity precondition | the tenant and subscription the provider resolved match the expected values | plan-time error carrying your message |
| Read through the same alias | the principal can actually read the target scope | authorization or not-found error before any create |
- Sign in to the sandbox tenant, then confirm the CLI reports the same tenant and subscription you wrote into the variables.
- Run
terraform initand keep the lock file, so the tested binaries are the ones in evidence. - Plan with the correct values. Expect one
terraform_dataobject and the identity output, and nothing else. - Re-plan with only the provider's subscription variable pointed at the untouched subscription. Expect your precondition message, a non-zero exit, and no resource read.
- Keep the wrong subscription and re-plan with
-refresh=false. The precondition still fails, because the identity read happens during planning and refresh suppression is not an identity control. - Restore the correct subscription and point the expected value at the untouched one. The precondition fails even though the credential is right, which is the copy-from-another-environment case.
- Remove the expected-value assertion and read a resource group through the alias instead. Expect an authorization error rather than a plan, proving that a valid token is not a permission.
- Sanitize the evidence: keep the tenant, a subscription suffix, the credential kind, and the issuer and subject if federation was used. Delete the plan files, the local state and the provider cache.
Expected observation. The correct run produces a one-object plan and an identity record whose tenant and subscription match the variables. Every wrong run stops before a create, and the failure names the gate that caught it rather than a downstream API error.
Failure check. If a wrong-scope plan succeeds, do not add conditions; find the leak: a resource that omits the provider and falls back to an unguarded default instance, a subscription exported in the shell, an assertion on the default alias while the resource uses another, or a data source skipped because its value was already in state.
Evidence to keep. The identity record, the alias table, the precondition transcript with its exit code, the provider version output, and a note naming which gate rejected which run. Together they reproduce the decision without publishing a live identifier.
Next in the Series
In Azure Networking: VNets, NSGs, Routes and Private Endpoint DNS, we'll explore Trace private service reachability through address spaces, routing, access rules and name resolution.