Skip to main content
This article explains how Terraform resources relate to each other, how values flow between resources, and how Terraform decides the order in which resources are created. Understanding these concepts is essential for reliable Azure deployments: most resources are not standalone—a subnet belongs to a VNet, a storage account lives in a resource group, and VMs depend on networking, disks, and identities. After reading this article you will clearly understand:
  • How Terraform builds its dependency graph.
  • When dependencies are discovered implicitly (via attribute references).
  • When you must be explicit using depends_on.
Basic VNet + Subnet example
/images/Python_Basics/section-name/Comments/frame_100.jpg
A virtual network (VNet) exposes attributes such as name, address_space, location, and resource_group_name. A subnet that belongs to that VNet should reference the VNet resource rather than hard-coding values. Referencing resource attributes lets Terraform both pass values and infer the ordering between resources (an implicit dependency). Example — VNet and Subnet with attribute references (implicit dependency):
Key points
  • When the azurerm_subnet block references azurerm_virtual_network.vnet.name, Terraform obtains that attribute value and also infers that the subnet depends on the VNet. This is an implicit dependency.
  • Attribute references both pass configuration values and create ordering information in Terraform’s dependency graph.
  • If you hard-code values or rely only on variables (for example resource_group_name = var.rg), Terraform cannot infer a dependency between the resources and may attempt parallel creation.
Why reference the resource group in each resource?
  • Terraform can create independent-looking resources in parallel. If a resource must be created inside a resource group but you supply only a variable (which is known at plan time), Terraform has no relationship to infer and may run creations concurrently. Referencing the resource group’s attributes (for example azurerm_resource_group.rg.name) makes the dependency explicit to Terraform (implicitly).
Implicit dependencies are sufficient in most scenarios
/images/Python_Basics/section-name/Comments/frame_100.jpg
Consider a resource group and a storage account; because the storage account references attributes from the resource group, Terraform ensures the resource group is created first.
When depends_on is required (explicit dependency)
  • Use depends_on when a logical dependency exists but Terraform cannot infer it through attribute references.
  • Common cases requiring depends_on: role assignments, policy assignments, diagnostic settings, private endpoints, resource associations, or provider behaviors where an ID is returned but the resource is not fully ready for dependent operations.
Example: NSG-to-subnet association that needs depends_on to handle Azure eventual consistency:
Use depends_on sparingly — only when Terraform cannot infer the dependency. Overusing depends_on makes configurations harder to read and less flexible. Always ask: is an attribute reference already providing the dependency?
A typical example configuration (main.tf)
/images/Python_Basics/section-name/Comments/frame_100.jpg
A compact main configuration that creates a resource group, virtual network, subnet, network security group (NSG), and the subnet-to-NSG association. This demonstrates implicit references plus a depends_on used for the association.
Provider and variables files
/images/Python_Basics/section-name/Comments/frame_100.jpg
Minimal provider.tf and variables.tf for the example:
Example terraform.tfvars used to pass values:
Commands: init, plan, apply
/images/Python_Basics/section-name/Comments/frame_100.jpg
From the directory with your Terraform files:
Example (cleaned) output showing correct ordering:
Explanation of the observed order
  • The resource group is created first because other resources reference it.
  • The VNet and NSG were created in parallel after the resource group because both depend only on the resource group (implicit dependency).
  • The subnet waited for the VNet to be created (implicit dependency via virtual_network_name).
  • The subnet-to-NSG association was applied last; even with attribute references, a depends_on can be used to guard against Azure timing issues.
Quick reference table: dependency discovery
Do not overuse depends_on. Using it everywhere defeats Terraform’s automatic dependency tracking, reduces parallelism, and makes configurations harder to maintain. Use depends_on only when you observe failures caused by provider eventual consistency or when there is no attribute reference to express the relationship.
Summary
  • Attribute references (for example azurerm_resource_group.rg.name or azurerm_virtual_network.vnet.id) allow values to flow between resources and automatically create implicit dependencies.
  • Terraform builds a dependency graph from those references and uses it to determine the correct creation order.
  • Use depends_on only when Terraform cannot infer a dependency from attribute references (for example, associations or operations that require a resource to be fully provisioned even when an ID is already available).
  • Avoid overusing depends_on to keep configurations readable, maintainable, and parallel where possible.
Understanding these mechanisms prevents race conditions and broken deployments in real production environments. Links and references

Watch Video