Microsoft Azure Learning Journey

Reuse Azure Infrastructure Safely with Modules and Deployment Stacks

Modules help teams reuse infrastructure; deployment stacks make resource ownership and cleanup explicit.

7 min read · 1218 words
Microsoft AzureInfrastructure as CodeTerraformBicepDevOps
Reuse Azure Infrastructure Safely with Modules and Deployment Stacks cover image

A shared network module saves teams from copying the same subnet rules, until a breaking change lands across every environment. A deployment stack cleans up a sandbox, until a missing resource is detached instead of deleted. Reuse and lifecycle controls help only when versioning, ownership, and deletion behavior are deliberate.

What you’ll learn

Where this fits

Module 25, Phase 6, Part 3. Continue from Terraform state and workflows; next: Fleet Automation and Hybrid Management.

Prerequisites

Modules are interfaces, not just folders

A module packages a resource boundary behind inputs and outputs. A network module might accept address ranges and return subnet IDs. An application module consumes those IDs without needing to know every internal network resource.

In Bicep, a module is another Bicep or ARM template deployed at a selected scope. You can reference a local file, a Bicep registry, or a template spec. Bicep modules are compiled into nested ARM templates. A module can expose outputs, use parameters, and deploy to a different supported scope.

In Terraform, a module is a collection of .tf files called by a root module. Child modules receive variables and return outputs; provider configuration and resource state remain managed by Terraform’s root state. Public registry modules, private registries, and source-control references can share implementations. Pin versions or immutable Git references and review updates before adopting them.

A stable contract keeps reuse useful

Abstraction is not free. A module that hides the Azure resource model, adds dozens of switches, or creates unexpected role assignments can be harder to operate than a small amount of repeated code.

Deployment stacks add lifecycle controls

An Azure deployment stack is an ARM resource that records a managed set of resources deployed from a template. It can apply deny settings to managed resources and define what happens when resources are removed from the template or the stack is deleted.

action-on-unmanage can detach resources or delete them according to the selected option and scope. A detached resource continues to exist but is no longer managed by that stack. Delete behavior is intentionally consequential: the stack can remove resources you did not expect if ownership boundaries are vague. Deny settings can also block legitimate operators, automation, and service operations until the policy is changed.

Stacks do not replace RBAC, resource locks, change review, backups, or Terraform state. Choose a stack boundary that matches a resource lifecycle. Keep shared DNS, network, and identity services in a different lifecycle from a disposable application environment. Review deployment operations and the stack’s managed-resource list before updating or deleting it.

Hands-on: inventory stacks without changing them

RESOURCE_GROUP="<EXISTING_DEVELOPMENT_RESOURCE_GROUP>"

az stack group list --resource-group "$RESOURCE_GROUP" \
  --query "[].{name:name, state:properties.provisioningState, denyMode:properties.denySettings.mode}" \
  --output table

az deployment group list --resource-group "$RESOURCE_GROUP" \
  --query "[].{name:name, state:properties.provisioningState, timestamp:properties.timestamp}" \
  --output table

These are management-plane inventories. An empty stack list does not imply that no infrastructure is deployed; ordinary ARM deployments and Terraform are managed differently.

✅ Checkpoint: For each stack, identify its template version, managed resources, deny mode, and what unmanage behavior would apply. Do not test deletion in a shared resource group.

Compare the tool boundaries

Teams can use more than one tool, but avoid managing the same resource from multiple state owners. A resource declared in Terraform and also submitted by Bicep can drift or be deleted unexpectedly.

Security, cost, and production

Review module source and release provenance before use. Pin versions, run static validation, inspect generated plans/what-if output, and apply through a least-privilege deployment identity. Secure private registries and state backends. Do not use a public module as trusted code without reviewing its resources and deployment scripts.

Modules do not add Azure resource charges by themselves. The resources they create can. Stacks can help clean disposable resources, but detach behavior can leave billable resources behind, and delete behavior can remove important data. Set retention and recovery protections outside the disposable stack where appropriate.

Troubleshooting

Five-pillar readiness checklist

Cleanup

No Azure resources were created. No cleanup is needed. If you test a deployment stack separately, inspect its managed resources and selected unmanage behavior before deleting it.

Key takeaways

Next: Manage Your Fleet with Azure Automation, Update Manager, and Arc.

Series navigation: Previous: Terraform on Azure · Current: Phase 6, Part 3 · Next: Fleet Automation

References

Share this article

LinkedIn post draft