- How Terraform builds its dependency graph.
- When dependencies are discovered implicitly (via attribute references).
- When you must be explicit using
depends_on.
/images/Python_Basics/section-name/Comments/frame_100.jpg
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):
- When the
azurerm_subnetblock referencesazurerm_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.
- 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).
/images/Python_Basics/section-name/Comments/frame_100.jpg
- Use
depends_onwhen 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.
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?/images/Python_Basics/section-name/Comments/frame_100.jpg
depends_on used for the association.
/images/Python_Basics/section-name/Comments/frame_100.jpg
provider.tf and variables.tf for the example:
terraform.tfvars used to pass values:
/images/Python_Basics/section-name/Comments/frame_100.jpg
- 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_oncan be used to guard against Azure timing issues.
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.- Attribute references (for example
azurerm_resource_group.rg.nameorazurerm_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_ononly 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_onto keep configurations readable, maintainable, and parallel where possible.
- Terraform CLI Docs
- Terraform Dependency Graph
- Azure Provider (azurerm) on Terraform Registry
- Azure Resource Manager documentation