Reusable modules¶
Design a small contract¶
A module is a directory, not a separate provider. Inputs define caller choices; outputs expose only what downstream code needs. Keep authentication in the root. Avoid a large collection of loosely related flags that hides important security behavior.
This repository includes three original Azure child modules:
| Module | Contract | Deliberate limit |
|---|---|---|
| Resource group | Name, location, tags | No subscription or role creation |
| Network | Name, group, region, address space, subnet map | No workload allow rules, NAT, VPN, or DNS |
| Storage | Name, group, region | No containers, endpoint, DNS, or data roles |
Use the catalog for the full source and module README pages.
Actual composition¶
The Azure foundation root calls the modules with local paths:
terraform {
required_version = ">= 1.7, < 2.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "= 5.14.0"
}
}
}
provider "azurerm" {
features {}
}
variable "prefix" {
description = "Short lowercase prefix for the resource group and network."
type = string
default = "learn"
validation {
condition = can(regex("^[a-z][a-z0-9-]{1,11}$", var.prefix))
error_message = "Use 2-12 lowercase letters, digits, or hyphens beginning with a letter."
}
}
variable "environment" {
description = "Environment boundary; use a separate state per environment."
type = string
default = "dev"
validation {
condition = contains(["dev", "test", "prod"], var.environment)
error_message = "Choose dev, test, or prod."
}
}
variable "location" {
description = "Approved Azure region."
type = string
default = "eastus2"
}
variable "storage_account_name" {
description = "Your globally unique lowercase alphanumeric storage name."
type = string
}
locals {
name = "${var.prefix}-${var.environment}"
tags = {
environment = var.environment
managedBy = "terraform"
}
}
module "group" {
source = "../../modules/azure-resource-group"
name = "rg-${local.name}"
location = var.location
tags = local.tags
}
module "network" {
source = "../../modules/azure-network"
name = "${local.name}-vnet"
resource_group_name = module.group.name
location = module.group.location
}
module "storage" {
source = "../../modules/azure-storage"
name = var.storage_account_name
resource_group_name = module.group.name
location = module.group.location
}
output "resource_group_name" {
description = "Dedicated group created by this root configuration."
value = module.group.name
}
output "storage_id" {
description = "Management-plane ID; no data access is granted."
value = module.storage.id
}
output "subnet_ids" {
description = "Named workload subnet IDs."
value = module.network.subnet_ids
}
The group outputs feed the network and storage inputs. This is both a documented interface and a dependency relationship. The root selects the provider version and configures the provider once.
Module scope and ownership¶
Decide which state owns each resource. Do not have a companion service template and this foundation manage the same VNet or storage account without an explicit state/import/migration plan. Do not duplicate a resource merely to expose it through another module.
For external modules, pin versioned releases and review their source, tests, license, and change history. Consider Azure Verified Modules for supported production building blocks; an overview exercise does not provide equivalent support guarantees.
Module tests¶
Tests here plan with mocked AzureRM. The storage test verifies that public networking, shared keys, and public containers remain disabled. The network test checks an NSG association for every requested subnet. Those are meaningful configuration contracts, not evidence that an Azure client can reach or use a deployed account.
Exercise: add a non-sensitive ownership tag input to a module, propagate it from the root, and add an assertion that would fail if propagation were removed.