Root contract¶
live/root.hcl is the file every unit includes. It holds the remote state backend,
the shared locals, the generated context, and the default providers, so that a
unit's own terragrunt.hcl stays down to a few lines.
Every unit opens with the same include:
expose = true republishes the root's locals inside the unit as
include.root.locals.<name>, which is how a unit reads the subscription and tenant
it is pointed at.
The locals block¶
root.hcl reads customer.hcl and the four scope files and flattens them into
eight values.
locals {
customer_vars = read_terragrunt_config(find_in_parent_folders("customer.hcl", "customer.hcl"), {
locals = {
tenant_root_id = "00000000-0000-0000-0000-000000000000"
business_unit = "platform"
amba_action_group_email = "alerts@example.com"
}
})
tenant_vars = read_terragrunt_config(find_in_parent_folders("tenant.hcl"))
subscription_vars = read_terragrunt_config(find_in_parent_folders("subscription.hcl"))
region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
env_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
tenant_id = local.tenant_vars.locals.tenant_id
subscription_id = local.subscription_vars.locals.subscription_id
location = local.region_vars.locals.location
location_short = local.region_vars.locals.location_short
environment = local.env_vars.locals.environment
tenant_root_id = local.customer_vars.locals.tenant_root_id
business_unit = local.customer_vars.locals.business_unit
amba_action_group_email = local.customer_vars.locals.amba_action_group_email
}
The lookup runs from the including unit's directory, not from live/. That is the
whole inheritance mechanism: the nearest subscription.hcl, region.hcl and
env.hcl above a unit are the ones that apply to it. None of them has a fallback,
so every unit needs all three above it.
The remote state backend¶
remote_state {
backend = "azurerm"
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
config = {
resource_group_name = get_env("BACKEND_AZURE_RESOURCE_GROUP_NAME", "placeholder-rg")
storage_account_name = get_env("BACKEND_AZURE_STORAGE_ACCOUNT_NAME", "placeholdersa")
container_name = get_env("BACKEND_AZURE_STORAGE_ACCOUNT_CONTAINER_NAME", "tfstate")
key = "${path_relative_to_include()}/terraform.tfstate"
subscription_id = get_env("AZURE_SUBSCRIPTION_ID", local.subscription_id)
tenant_id = local.tenant_id
use_azuread_auth = true
}
}
Four things here are worth understanding.
The backend file is generated. Terragrunt writes backend.tf into the unit at run
time and overwrites it every run. It is in .gitignore and must never be committed.
The state key is the unit's path. Each unit gets its own state file, and the folder path is the key.
The backend subscription is read separately from the deploy subscription. The
state account lives in the management subscription, which is AZURE_SUBSCRIPTION_ID.
A unit's deploy subscription can be different. Connectivity, for example, deploys
into its own subscription while sharing the one state account.
Access is by Entra ID. use_azuread_auth = true, no storage account keys. The
bootstrap disables shared key access on the account.
Placeholders and offline validation¶
Every get_env has a placeholder fallback. The coordinates come from the AZURE_*
and BACKEND_* Action variables the bootstrap set, which the reusable workflows
export into the run. The fallbacks exist so the tree still generates and validates
with no Azure access at all.
Validating offline takes two commands, in this order:
terragrunt --working-dir live/<unit> init -backend=false
TG_NO_AUTO_INIT=true terragrunt --working-dir live/<unit> validate
Terragrunt initializes before it validates, so a plain terragrunt validate sets
up the real azurerm backend and fails to authorize against the placeholder tenant.
init -backend=false populates the run directory without touching Azure, and
TG_NO_AUTO_INIT stops Terragrunt initializing again on top of it.
That is why a freshly generated repository validates before anything is configured.
Without live/customer.hcl, as in the template itself, root.hcl falls back to
placeholder customer values, and the landing-zones unit rejects the all-zero
tenant_root_id, so an unstamped tree never deploys a hierarchy.
It is also why you do not edit backend names in this file. They arrive from the
environment.
The default providers¶
root.hcl generates a providers.tf carrying azurerm ~> 4.0, azapi ~> 2.12,
and azuread ~> 3.10. azurerm and azapi are pinned to the unit's own
subscription and tenant. azuread sets only tenant_id, because Entra ID objects
belong to the tenant, not to a subscription. Neither azurerm nor azapi
registers resource providers: azurerm sets
resource_provider_registrations = "none" and azapi sets
skip_provider_registration = true.
That is the set most units need. A unit needing a different set declares its own
generate "provider" block and sets merge_strategy = "deep" on its include:
The deep merge is required, not optional
Without it, two generate blocks with the same name are a hard error, not an
override. The landing-zones and amba units both rely on this because they
need the alz provider.
root.hcl closes with the version floors: terraform_version_constraint = ">= 1.12"
and terragrunt_version_constraint = ">= 1.0".
The generated context¶
The locals above are Terragrunt values. Terraform cannot read them. A third
generate block bridges that gap by writing a context.tf into every unit, the
same way backend.tf and providers.tf are written:
generate "context" {
path = "context.tf"
if_exists = "overwrite_terragrunt"
contents = <<-EOF
locals {
context = {
tenant_id = ${jsonencode(local.tenant_id)}
tenant_root_id = ${jsonencode(local.tenant_root_id)}
business_unit = ${jsonencode(local.business_unit)}
amba_action_group_email = ${jsonencode(local.amba_action_group_email)}
subscription_id = ${jsonencode(local.subscription_id)}
location = ${jsonencode(local.location)}
location_short = ${jsonencode(local.location_short)}
environment = ${jsonencode(local.environment)}
}
}
EOF
}
A main.tf then reads the inherited scope as plain Terraform:
locals {
management_resource_group_name = "rg-management-${local.context.location_short}"
}
module "management" {
location = local.context.location
}
Three things follow from this.
Each value has one source. Changing the region is a one file edit. Every unit
under that region scope picks up the new location and location_short on the
next run. The foundation's region.hcl takes them from customer.hcl, and the
businessunit tag and the AMBA inbox reach the foundation units through
local.context.business_unit and local.context.amba_action_group_email.
The values are namespaced under one context object on purpose. A unit is
free to declare its own location local, and local.context.location never
collides with it. A bare location local generated into the unit would.
context.tf is generated at run time and gitignored. It is build output, like
backend.tf and providers.tf. Never commit it.
Cross-scope values are not in the context
The context carries only what the unit inherits through its own ancestors.
connectivity_subscription_id is not in it, because the connectivity
subscription is not an ancestor of _foundation/landing-zones or of the corp
spoke network unit. Both keep it as a literal in their main.tf.
customer.hcl and tenant.hcl¶
Both sit at the live/ root deliberately. find_in_parent_folders only walks
ancestors, so each has to be an ancestor of every unit.
live/customer.hcl holds the customer values. The bootstrap writes it once, when it
creates the repository, and the repository owns it from then on; the template ships
without it. See customer.hcl for how
the bootstrap fills it in.
locals {
tenant_root_id = "<management group name>"
location = "westeurope"
location_short = "weu"
business_unit = "<short name>"
amba_action_group_email = "alerts@customer.example"
}
tenant_root_id is the management group the ALZ hierarchy is built under, as the
plain name. The tenant root group's name is the tenant id. The landing-zones unit
fails its plan if the value is empty or the all-zero placeholder.
live/tenant.hcl carries one value:
tenant_id is the Entra ID tenant the deployment authenticates against. In CI it
comes from the AZURE_TENANT_ID variable, so the placeholder is used only for local
runs.
region.hcl, env.hcl and subscription.hcl¶
The foundation's copies sit at live/_foundation/.
The foundation's region.hcl takes the primary region from customer.hcl:
customer_vars = read_terragrunt_config(find_in_parent_folders("customer.hcl", "customer.hcl"), {
locals = { location = "westeurope", location_short = "weu" }
})
location = local.customer_vars.locals.location
location_short = local.customer_vars.locals.location_short
A region.hcl elsewhere in the tree sets its own location and location_short.
To move a subtree to another region, edit the region.hcl at that scope; for the
foundation, edit customer.hcl.
env.hcl sets the environment. The foundation's copy is a fixed value in the tree:
Each platform subscription folder and each landing zone folder has its own
env.hcl. A landing zone that hosts more than one environment in one subscription
adds a folder per environment under its region folder, each with its own env.hcl.
The value is the short form (prod, staging, dev). It feeds resource names and
the env tag.
These values reach Terraform through the generated context, so the units read them rather than restating them.
subscription.hcl sets the deploy subscription, again with an environment lookup and
a placeholder fallback:
The foundation is operated from the management subscription, because that is where the deploy identity holds tenant-root rights and where the state backend lives. The management groups and policy it deploys are tenant-scoped, not resources in that subscription, but every unit still authenticates against it.
What you edit here¶
Almost nothing. root.hcl is driven entirely by variables the bootstrap set. Touch it
only to change the Terraform binary, raise a version floor, or add a provider to the
default set. The customer values live in customer.hcl. Get tenant_root_id right
before the first apply: changing it later moves the whole hierarchy to a new
parent, which is a migration, not an edit.