A data source lets Terraform fetch information from outside resources or existing infrastructure to use in your configuration.
Click to reveal answer
beginner
Why do data source dependencies matter in Terraform?
Because Terraform needs to know the order to fetch data and create resources, dependencies ensure data sources are read before resources that use them.
Click to reveal answer
intermediate
How does Terraform know the dependency order between data sources and resources?
Terraform automatically detects dependencies when a resource references a data source's attributes in its configuration.
Click to reveal answer
intermediate
What happens if a resource uses a data source that depends on another resource?
Terraform creates a dependency chain, ensuring the first resource is created, then the data source reads updated info, then the dependent resource is created.
Click to reveal answer
advanced
How can you manually add dependencies between data sources and resources in Terraform?
You can use the 'depends_on' argument in a resource or data source block to explicitly declare dependencies.
Click to reveal answer
What does a Terraform data source do?
ADeletes resources
BFetches existing infrastructure information
CCreates new cloud resources
DRuns scripts on servers
✗ Incorrect
Data sources fetch information from existing infrastructure or external systems for use in Terraform.
How does Terraform detect dependencies between data sources and resources?
ABy manual user input only
BBy the order of blocks in the file
CBy file name alphabetically
DBy resource references to data source attributes
✗ Incorrect
Terraform automatically detects dependencies when a resource references data source attributes.
Which argument can you use to explicitly declare dependencies in Terraform?
Adepends_on
Bdepends
Crequire
Dneeds
✗ Incorrect
'depends_on' is the correct argument to manually specify dependencies.
If a resource uses data from a data source that depends on another resource, what happens?
ATerraform creates resources in the correct order automatically
BTerraform creates all resources simultaneously
CTerraform throws an error
DTerraform ignores the dependency
✗ Incorrect
Terraform builds a dependency chain and creates resources in the right order.
Can data sources create or modify infrastructure?
AYes, they delete resources
BYes, they create resources
CNo, they only read existing data
DYes, they modify resources
✗ Incorrect
Data sources only read existing infrastructure information; they do not create or modify resources.
Explain how Terraform manages dependencies between data sources and resources.
Think about how Terraform knows what to create first.
You got /4 concepts.
Describe a scenario where you might need to use depends_on with a data source.
Consider when automatic detection is not enough.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using data source dependencies in Terraform?
easy
A. To fetch existing resource information before creating new resources
B. To delete resources automatically after creation
C. To speed up Terraform plan execution by skipping resources
D. To create duplicate resources with the same configuration
Solution
Step 1: Understand data source role
Data sources in Terraform are used to read information about existing resources outside Terraform or created by other means.
Step 2: Identify dependency purpose
Dependencies ensure Terraform fetches this existing info before creating or modifying other resources that rely on it.
Final Answer:
To fetch existing resource information before creating new resources -> Option A
Quick Check:
Data source dependencies = fetch info first [OK]
Hint: Data sources get info before resource creation [OK]
Common Mistakes:
Thinking data sources create or delete resources
Confusing dependencies with resource lifecycle
Assuming data sources speed up plans by skipping
2. Which syntax correctly adds an explicit dependency on a data source named aws_vpc.main in a resource block?
easy
A. depends_on = {aws_vpc.main}
B. depends_on = aws_vpc.main
C. depends_on = ["aws_vpc.main"]
D. depends_on = [aws_vpc.main]
Solution
Step 1: Recall depends_on syntax
Terraform expects depends_on to be a list of resource or data source references without quotes.
Step 2: Analyze options
depends_on = [aws_vpc.main] uses a list with the correct reference syntax. depends_on = aws_vpc.main misses list brackets. depends_on = ["aws_vpc.main"] uses quotes making it a string, not a reference. depends_on = {aws_vpc.main} uses braces which is invalid.
Final Answer:
depends_on = [aws_vpc.main] -> Option D
Quick Check:
depends_on requires list of references [OK]
Hint: Use square brackets and no quotes for depends_on [OK]
Common Mistakes:
Using quotes around resource names
Omitting square brackets for depends_on
Using curly braces instead of square brackets
3. Given this Terraform snippet:
data "aws_ami" "example" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.example.id
instance_type = "t2.micro"
}
What happens if the data source aws_ami.example fails to find a matching AMI?
medium
A. Terraform will skip the instance creation silently
B. Terraform will create the instance with a default AMI
C. Terraform plan will fail with an error before creating the instance
D. Terraform will retry the data source until it finds an AMI
Solution
Step 1: Understand data source failure behavior
If a data source cannot find the requested resource, Terraform plan fails with an error because it cannot resolve the value.
Step 2: Check resource dependency
The resource depends on the data source's output for the AMI ID, so without it, resource creation cannot proceed.
Final Answer:
Terraform plan will fail with an error before creating the instance -> Option C
Quick Check:
Data source failure = plan error [OK]
Hint: Missing data source value causes plan failure [OK]
Hint: Avoid depends_on referencing the same resource [OK]
Common Mistakes:
Confusing data source filter syntax as error
Thinking subnet_id cannot use data sources
Assuming instance_type is invalid without checking
5. You want to create an AWS security group that allows inbound traffic only from instances in a specific subnet fetched via a data source. Which approach correctly ensures Terraform creates the security group only after the subnet data is fetched?
hard
A. Use depends_on = [data.aws_subnet.target] in the security group resource
B. No need for depends_on; Terraform infers dependency from data source usage
C. Create the security group first, then fetch the subnet data source
D. Use a null_resource to delay security group creation
Solution
Step 1: Understand implicit dependencies
When a resource uses a data source attribute (like subnet ID), Terraform automatically creates a dependency to fetch data first.
Step 2: Evaluate explicit depends_on necessity
Explicit depends_on is not needed unless the dependency is indirect or complex; here, implicit dependency suffices.
Final Answer:
No need for depends_on; Terraform infers dependency from data source usage -> Option B
Quick Check:
Data source usage creates implicit dependency [OK]
Hint: Data source references create implicit dependencies [OK]
Common Mistakes:
Adding unnecessary depends_on for direct data source use
Creating resources in wrong order ignoring implicit dependencies
Using null_resource unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.