0
0
Terraformcloud~30 mins

Resource dependencies (implicit) in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Terraform Resource Dependencies (Implicit)
📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You want to create a virtual network and then create a virtual machine inside that network. The virtual machine must be created only after the network is ready.
🎯 Goal: Build a Terraform configuration that creates a virtual network and a virtual machine. Use implicit resource dependencies so Terraform knows to create the network before the virtual machine.
📋 What You'll Learn
Create a resource called azurerm_virtual_network named main_network with address space 10.0.0.0/16.
Create a resource called azurerm_subnet named main_subnet inside main_network with address prefix 10.0.1.0/24.
Create a resource called azurerm_network_interface named main_nic attached to main_subnet.
Create a resource called azurerm_linux_virtual_machine named main_vm that uses main_nic.
Use implicit dependencies by referencing resource attributes to ensure correct creation order.
💡 Why This Matters
🌍 Real World
Cloud engineers often create networks and virtual machines that depend on each other. Using implicit dependencies in Terraform helps automate the correct creation order without extra configuration.
💼 Career
Understanding resource dependencies is essential for infrastructure as code roles, ensuring reliable and maintainable cloud deployments.
Progress0 / 4 steps
1
Create the virtual network resource
Create a resource block for azurerm_virtual_network named main_network with the address space set to ["10.0.0.0/16"].
Terraform
Need a hint?

Use resource "azurerm_virtual_network" "main_network" { ... } and set address_space to ["10.0.0.0/16"].

2
Add a subnet resource inside the virtual network
Create a resource block for azurerm_subnet named main_subnet with the address prefix "10.0.1.0/24". Reference the virtual network ID from azurerm_virtual_network.main_network.id to attach the subnet to the network.
Terraform
Need a hint?

Reference the virtual network name using azurerm_virtual_network.main_network.name inside the subnet resource.

3
Create a network interface attached to the subnet
Create a resource block for azurerm_network_interface named main_nic. Set the subnet_id to azurerm_subnet.main_subnet.id to attach it to the subnet.
Terraform
Need a hint?

Inside ip_configuration, set subnet_id to azurerm_subnet.main_subnet.id.

4
Create a Linux virtual machine using the network interface
Create a resource block for azurerm_linux_virtual_machine named main_vm. Set the network_interface_ids to a list containing azurerm_network_interface.main_nic.id. Use name as "main-vm", resource_group_name as "myResourceGroup", and location as "eastus". Set size to "Standard_DS1_v2". Use admin_username as "adminuser" and admin_password as "Password1234!". Set os_disk with caching as "ReadWrite" and storage_account_type as "Standard_LRS". Use source_image_reference for Ubuntu 20.04 LTS with publisher "Canonical", offer "UbuntuServer", sku "20_04-lts", and version "latest". This will implicitly depend on the network interface.
Terraform
Need a hint?

Reference the network interface ID in network_interface_ids to create an implicit dependency.