Azure network and application modules
Start with an architecture, not a resource list
Imagine an internal catalog application used by employees. It needs a web runtime, a database for records, object storage for attachments, a vault for secrets and a monitoring destination. We want private access to the application and its data services, a clear boundary between development and production, and no credentials in configuration. Azure App Service is one candidate for hosting; a VM offers operating-system control but needs patching, and AKS offers orchestration with additional operational responsibilities. Choose a service based on the workload, not on Terraform syntax. No Azure account is required for this lesson.
Approved private client -> app private endpoint -> App ServiceApp Service -> integration subnet -> data private endpointsPrivate DNS -> endpoint addresses for vault / storage / databaseApp managed identity -> service-specific authorizationDiagnostic settings -> separate approved monitoring destinationThe arrows describe intended traffic or authorization, not tested network connections. Each endpoint needs an approved target, DNS resolution, routing and service permissions. The monitoring destination may belong to a separate resource group and owner.
Divide the address space and verify routes
A virtual network provides an address boundary; subnets divide it into smaller ranges. Network security groups (NSGs) filter supported traffic by rules, but do not grant application identities permission to read data. Use a distinct delegated integration subnet for App Service where required and separate subnets for private endpoints and any VMs. The following complete local configuration calculates three /24 ranges. It creates no Azure resources and uses no external provider. The CLI pin is provisional; the expected output has been checked with Python rather than Terraform.
terraform { required_version = "= 1.14.8"}locals { network = "10.70.0.0/16" subnets = { integration = cidrsubnet(local.network, 8, 10) endpoints = cidrsubnet(local.network, 8, 20) compute = cidrsubnet(local.network, 8, 30) }}output "subnets" { value = local.subnets}The expected prefixes are 10.70.10.0/24, 10.70.20.0/24 and 10.70.30.0/24. They do not overlap. This is address arithmetic, not a guarantee of available Azure capacity: actual subnet reservations, delegation, scaling requirements, peer networks and policies must be checked before deployment. Review route tables and effective NSG rules rather than assuming that a subnet name makes traffic private.
Separate inbound from outbound connectivity
App Service virtual network integration allows outbound access into a virtual network; it does not make the app itself privately reachable. Use a supported private endpoint for inbound private connectivity and review public network access separately. App Service integration and its private endpoint cannot share the same subnet. A private endpoint is a network interface with a private address linked to a particular service. A private endpoint for a vault does not automatically make storage or a database private. Storage may need distinct endpoints for the services used, such as blob and file. Managed databases have service-specific private network modes and DNS requirements.
# Static AzureRM fragment; requires declared variables and approved schema.resource "azurerm_private_endpoint" "vault" { name = "pe-catalog-vault" location = var.location resource_group_name = var.resource_group_name subnet_id = var.endpoint_subnet_id private_service_connection { name = "vault-connection" private_connection_resource_id = var.vault_id subresource_names = ["vault"] is_manual_connection = false } private_dns_zone_group { name = "vault-dns" private_dns_zone_ids = [var.vault_private_dns_zone_id] }}This fragment illustrates a vault endpoint with a DNS zone group. It is not a full configuration: the vault, VNet-linked private DNS zone, subnet and inputs must exist or be managed by other modules. Review the exact provider schema and service configuration. The DNS zone group associates records with the endpoint, but clients must use a resolver that can resolve those records. Test the service hostname from the intended client; using a raw IP can break TLS hostname validation. Private connectivity does not by itself disable public access.
Compose modules using explicit interfaces
Organize the root module around ownership: network produces subnet IDs; application consumes the integration subnet ID and exposes its principal ID; data consumes the endpoint subnet ID and runtime principal ID. Keep the module graph acyclic. If the application also needs a database URL, wire that through a separate configuration or authorization module, or split the data resource from its role assignments so neither module depends on the other in a cycle.
# Illustrative interfaces only; these child modules are not supplied.module "network" { source = "./modules/network" cidr = "10.70.0.0/16"}module "application" { source = "./modules/application" integration_subnet_id = module.network.integration_subnet_id}module "data" { source = "./modules/data" endpoint_subnet_id = module.network.endpoint_subnet_id app_principal_id = module.application.principal_id}This is a contract sketch, not a downloadable module package. A real network module would export resource IDs rather than relying on names or constructing assumed IDs. A module is not a state or authorization boundary by default. Separate root configurations and backend access where different teams must manage long-lived shared networking or data independently. Validate the output types, ownership and migration impact before changing published interfaces.
Grant the application only its runtime permissions
A deployment identity needs narrowly scoped management-plane permissions to create approved resources; an application needs data-plane rights to perform its actual tasks. For example, an App Service managed identity can read a Key Vault secret with an appropriate role when the vault uses Azure RBAC. Its principal ID is not a secret. The following role assignment belongs with the access-policy owner, not necessarily the app module.
# Static fragment; assumes Key Vault uses Azure RBAC authorization.resource "azurerm_role_assignment" "app_read" { scope = var.vault_id role_definition_name = "Key Vault Secrets User" principal_id = var.app_principal_id}The deployment identity needs separate authorization to create this assignment; do not grant broad Owner rights just to avoid a permission error. Check role propagation and network access before diagnosing an application failure. For storage, grant the service-specific data role required by the app; for a managed database, establish supported database authentication and privileges. Do not fetch secret values into Terraform merely to pass them to an app, because they can be stored in state. Prefer runtime retrieval or supported secret references. AKS uses distinct cluster and workload identity concepts; for pods, evaluate Microsoft Entra Workload ID and the target service authorization rather than reusing a cluster-wide identity.
Review the plan and define acceptance tests
Before any authorized deployment, reject unintended public access, overly broad roles, overlapping networks and unexpected resource replacement. Check whether the app runtime and database tier support the desired private network mode in the chosen region. Define tests for correct DNS resolution from the app network, allowed database access for the app identity, denied access for an unrelated identity and unavailable public access where intended. Diagnostic settings should export only necessary categories to a reviewed destination with controlled access and retention. A successful Terraform apply does not establish that these runtime tests passed. In the offline exercise, predict expected results and mark every live check as pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Azure network and application modules — knowledge check
Review Azure architecture, identity and lifecycle.
Which feature gives an App Service app outbound connectivity into a virtual network?
What must be checked after creating a private endpoint for a vault?
Which output should a network module provide to an app module?
The app has a managed identity but cannot read a vault secret. What should you review?
What is a sensible way to avoid a module cycle when assigning app access to a database?