0
0
Terraformcloud~30 mins

Data source dependencies in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Data source dependencies in Terraform
📖 Scenario: You are setting up infrastructure using Terraform. You want to create a virtual machine that depends on the network information retrieved from a data source. This ensures the VM uses the correct network ID.
🎯 Goal: Build a Terraform configuration that uses a data source to get network details and then creates a virtual machine resource that depends on this data source.
📋 What You'll Learn
Create a data source block to fetch network information
Define a variable for the network name
Create a virtual machine resource that uses the network ID from the data source
Ensure the VM resource depends on the data source
💡 Why This Matters
🌍 Real World
In real cloud projects, resources often depend on existing infrastructure. Using data sources and dependencies ensures correct order and configuration.
💼 Career
Understanding data source dependencies is essential for cloud engineers and DevOps professionals managing infrastructure as code.
Progress0 / 4 steps
1
Create a data source to fetch network information
Write a Terraform data block named network using the provider azurerm_network_interface. Set the name attribute to "my-nic" and the resource_group_name attribute to "my-resource-group".
Terraform
Need a hint?

Use a data block with the exact name network and provider azurerm_network_interface.

2
Define a variable for the network name
Create a Terraform variable named network_name with a default value of "my-nic".
Terraform
Need a hint?

Use a variable block with the exact name network_name and set its default to "my-nic". Update the data source to use var.network_name for the name.

3
Create a virtual machine resource using the network ID
Add a Terraform resource block named azurerm_linux_virtual_machine with the resource name vm. Set the name to "my-vm", resource_group_name to "my-resource-group", location to data.azurerm_network_interface.network.location, and network_interface_ids to a list containing data.azurerm_network_interface.network.id.
Terraform
Need a hint?

Use a resource block with the exact name azurerm_linux_virtual_machine and resource name vm. Use the network ID from the data source in network_interface_ids.

4
Add explicit dependency on the data source
Add the depends_on attribute to the azurerm_linux_virtual_machine.vm resource. Set it to a list containing data.azurerm_network_interface.network to ensure the VM depends on the network data source.
Terraform
Need a hint?

Add the depends_on attribute inside the VM resource block with the exact value [data.azurerm_network_interface.network].