Chapter 2 · Modules and a private registry
A Private Terraform Module Registry
What it is
Every terraform-azurerm-* and terraform-azuredevops-* module in this
platform follows the same structure: main.tf / variables.tf /
outputs.tf / locals.tf / versions.tf always present, feature-specific
files split out by name (diag.tf, privatelink.tf, eventgrid.tf, ...
rather than one growing main.tf), a VERSION file and CHANGELOG.md
following Keep a Changelog and Semantic Versioning, and a working
examples/basic that runs with just the required inputs. Each module
publishes itself to a private Terraform Cloud registry through its own
.azuredevops/pipeline.yaml, extending a shared
platform/terraform/module-registry/publish.yaml template.
Why it exists
At work we use Terraform Enterprise this way: modules in their own repos, versioned, published to a private registry that every team pulls from. For this platform I wanted the same setup without an enterprise licence, so I used Terraform Cloud's free private registry. For what I need it behaves the same. Each module publishes itself from its own pipeline, using a Terraform Cloud token kept as a secret pipeline variable, and a repo pins the version it wants and moves up when it's ready.
The alternatives were copying Terraform between repos, where every copy drifts a little, or public registry modules, which are built for everyone and don't know my naming, tags or how diagnostics should be wired.
Key decisions
- Naming convention is enforced, not just documented. Every module
computes its resource name as
<workload>-<environment>-<location_code>-<suffix>(or the compressed alnum-only form for resources like storage accounts), with aname_overrideescape hatch that still appends the module's own suffix rather than replacing the whole convention. The environment segment is left out when no environment is passed. - A standard variable set on every module that deploys a resource —
resource_group_name,location(restricted to specific Azure regions),workload,name_override,tags, and the optionalenvironment,costcenterandprojectcode— validated the same way, so consuming one feels like consuming another. Modules that only grant access or write into a parent they don't own, like role assignments or Key Vault secrets, take the parent's ID and tags instead. - A module works like a function. It checks the shape of what it's
given (types, allowed values, unknown keys) and then does exactly what
it's told. It doesn't try to protect callers from a value that's valid
but wrong, like an
environmentcopied from another repo. At work I've seen where the other way leads: every consumer mistake becomes a change to the shared module, and the platform team spends its time fixing other people's calls. If the input is wrong, the fix is in the call. - Restrictions belong in Azure Policy, not in the module. A policy can grant an exemption when a team has a real reason to do something different. A module can't, so every rule baked into it turns into a fight with the next consumer who has a good use case. That's also why no module ignores tag changes: cost centers and project codes change when the company changes, and whoever passes the value owns it.
- Every change that ships is a new version. The publish pipeline
reads
VERSION, publishes it and tags the commit, and it fails if that version already exists. So a change can't quietly replace what a repo already pinned. Consumers on~> 1.0pick up fixes on their next plan, and a breaking change means a new major they move to on purpose. - Some tags belong to Azure, not to the module. The portal links a web
app to its Application Insights through a
hidden-linktag that Azure adds itself. Because the module owns the app's tags, every apply removed it. The App Service module now takes the Application Insights ID as an input and writes that tag itself, so the link survives every apply.
Current state
Thirty terraform-azurerm-* modules, an Entra service principal module,
and nine terraform-azuredevops-* modules that
manage Azure DevOps itself. The landing zone consumes the governance and network modules
(management group, resource groups, role definitions and assignments,
VNet, private DNS, Log Analytics, budgets). This site's infrastructure
consumes App Service, App Service plan, Application Insights, managed
identity and, since the AI work, the cognitive services module.
The two newest modules show where the convention bends. Azure OpenAI and
AI Foundry are one product in the portal, but I split them into
terraform-azurerm-cognitive (the account, model deployments,
diagnostics) and terraform-azurerm-ai-foundry (projects and their
connections). The Foundry project features aren't in the azurerm
provider yet and need azapi. Keeping them in their own module means
anything that just needs a model endpoint, like this site, never takes on
that second provider. The Foundry module takes the account's ID as an
input, the same way the Key Vault secret module takes a vault's ID.