📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You want to create two resources: a virtual network and a virtual machine. The virtual machine must only be created after the virtual network is ready.
🎯 Goal: Build a Terraform configuration where the virtual machine resource explicitly depends on the virtual network resource using depends_on.
📋 What You'll Learn
Create a virtual network resource named my_vnet with address space 10.0.0.0/16.
Create a virtual machine resource named my_vm.
Use depends_on in the virtual machine resource to explicitly depend on the virtual network resource.
💡 Why This Matters
🌍 Real World
In real cloud projects, some resources must be created only after others are ready. Using depends_on ensures Terraform respects these dependencies.
💼 Career
Cloud engineers and DevOps professionals use depends_on to control resource creation order and avoid deployment errors.
Progress0 / 4 steps
1
Create the virtual network resource
Create a Terraform resource block named azurerm_virtual_network.my_vnet with the address space set to ["10.0.0.0/16"].
Terraform
Hint
Use resource "azurerm_virtual_network" "my_vnet" { ... } and set address_space to ["10.0.0.0/16"].
2
Create the virtual machine resource
Add a Terraform resource block named azurerm_virtual_machine.my_vm with a placeholder configuration (name, location, resource group). Do not add depends_on yet.
Terraform
Hint
Create resource "azurerm_virtual_machine" "my_vm" { ... } with basic properties like name, location, and resource_group_name.
3
Add depends_on to the virtual machine
Inside the azurerm_virtual_machine.my_vm resource block, add depends_on = [azurerm_virtual_network.my_vnet] to explicitly depend on the virtual network.
Terraform
Hint
Add depends_on = [azurerm_virtual_network.my_vnet] inside the my_vm resource block.
4
Complete the Terraform configuration
Add the required provider block for AzureRM with version constraint "~> 3.0" and a resource group resource named myResourceGroup with location East US.
Terraform
Hint
Add provider "azurerm" { version = "~> 3.0" } and a resource group resource named myResourceGroup with location East US.
Practice
(1/5)
1. What is the main purpose of depends_on in Terraform?
easy
A. To delete resources automatically
B. To define the resource type
C. To set resource properties
D. To explicitly specify the order in which resources are created
Solution
Step 1: Understand resource creation order
Terraform creates resources in parallel by default, but sometimes one resource must be created before another.
Step 2: Role of depends_on
The depends_on attribute explicitly tells Terraform to wait for one resource before creating another.
Final Answer:
To explicitly specify the order in which resources are created -> Option D
Quick Check:
depends_on controls creation order = A [OK]
Hint: depends_on sets creation order explicitly [OK]
Common Mistakes:
Confusing depends_on with resource type definition
Thinking depends_on sets resource properties
Assuming depends_on deletes resources
2. Which of the following is the correct syntax to add depends_on for a resource named aws_instance.example?
easy
A. depends_on = aws_instance.example
B. depends_on = {aws_instance.example}
C. depends_on = [aws_instance.example]
D. depends_on = ["aws_instance.example"]
Solution
Step 1: Understand depends_on syntax
The depends_on attribute expects a list of resource references.
Step 2: Correct list format
Resources must be inside square brackets as unquoted references, like [aws_instance.example].
Final Answer:
depends_on = [aws_instance.example] -> Option C
Quick Check:
depends_on requires list of resource references = B [OK]
Hint: Use list of unquoted resource references for depends_on [OK]
Common Mistakes:
Omitting square brackets around resource reference
Using curly braces instead of square brackets
Not using a list format
3. Given the following Terraform snippet, what will happen during apply?
A. depends_on should be a list of resource references
B. instance attribute is incorrect
C. Resource names cannot be used in depends_on
D. The AMI ID is invalid
Solution
Step 1: Check depends_on syntax
The depends_on attribute must be a list, even if it has one resource.
Step 2: Identify error in code
Here, depends_on = aws_instance.web is missing square brackets, causing a syntax error.
Final Answer:
depends_on should be a list of resource references -> Option A
Quick Check:
depends_on needs list syntax = A [OK]
Hint: Always use square brackets for depends_on list [OK]
Common Mistakes:
Using single resource without list brackets
Confusing depends_on with resource attributes
Ignoring Terraform syntax requirements
5. You have two resources: aws_db_instance.db and aws_ecs_service.ecs. The ECS service must start only after the database is fully created. How should you configure depends_on in aws_ecs_service.ecs to ensure this?
hard
A. depends_on = aws_db_instance.db
B. depends_on = [aws_db_instance.db]
C. depends_on = ["aws_db_instance.db"]
D. depends_on = {"aws_db_instance.db"}
Solution
Step 1: Understand depends_on format
Terraform requires depends_on to be a list of resource references.
Step 2: Correct usage for ECS service
To make ECS depend on the DB, use depends_on = [aws_db_instance.db] inside the ECS resource block.
Final Answer:
depends_on = [aws_db_instance.db] -> Option B
Quick Check:
depends_on list of resource references for explicit dependency = A [OK]
Hint: Use list of unquoted resource references for depends_on [OK]
Common Mistakes:
Omitting square brackets around resource reference
Omitting square brackets
Using curly braces instead of brackets
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.