What is the main purpose of data sources in Terraform?
Data sources let Terraform read information from existing infrastructure outside of its control, so it can use that info when creating or updating resources.
Click to reveal answer
beginner
How do data sources help avoid duplication in infrastructure management?
They allow Terraform to reference existing resources instead of creating new ones, preventing duplicate resources and conflicts.
Click to reveal answer
intermediate
Why is querying existing infrastructure important before provisioning new resources?
It ensures Terraform has up-to-date info about current resources, so it can make decisions that keep infrastructure consistent and avoid errors.
Click to reveal answer
beginner
Give an example of when you would use a data source in Terraform.
When you want to get the ID of an existing network or security group to attach to a new server, you use a data source to query that info.
Click to reveal answer
intermediate
What happens if Terraform does not query existing infrastructure before applying changes?
Terraform might create duplicate resources, overwrite settings, or fail due to conflicts, leading to unstable infrastructure.
Click to reveal answer
What does a Terraform data source do?
AReads info from existing infrastructure
BCreates new cloud resources
CDeletes unused resources
DRuns scripts on servers
✗ Incorrect
Data sources read existing infrastructure info; they do not create or delete resources.
Why use data sources instead of hardcoding resource IDs?
ATo delete old resources automatically
BTo speed up resource creation
CTo avoid using variables
DTo keep Terraform configs dynamic and accurate
✗ Incorrect
Data sources keep configs dynamic by fetching current info, avoiding errors from hardcoded values.
Which problem can occur if Terraform ignores existing infrastructure?
ADuplicate resources may be created
BTerraform will run faster
CResources will be deleted automatically
DNo impact on infrastructure
✗ Incorrect
Ignoring existing infrastructure can cause Terraform to create duplicates, causing conflicts.
When is it best to use a data source in Terraform?
AWhen creating brand new resources
BWhen deleting resources
CWhen referencing resources created outside Terraform
DWhen running Terraform plan only
✗ Incorrect
Data sources are used to reference existing resources not managed by Terraform.
What kind of info can a Terraform data source provide?
AUser login credentials
BResource IDs, names, and attributes
CTerraform version info
DCloud provider billing details
✗ Incorrect
Data sources provide details like IDs and attributes of existing resources.
Explain why Terraform uses data sources to query existing infrastructure before creating new resources.
Think about how Terraform knows what already exists.
You got /4 concepts.
Describe a scenario where using a Terraform data source is necessary and beneficial.
Consider when you need info from resources created outside Terraform.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason Terraform uses data sources to query existing infrastructure?
easy
A. To get information about resources created outside Terraform
B. To create new resources automatically
C. To delete unused resources
D. To backup infrastructure state
Solution
Step 1: Understand Terraform data sources purpose
Data sources allow Terraform to read details about resources that exist but were not created by the current Terraform configuration.
Step 2: Identify the correct use case
Since data sources do not create or delete resources, their main use is to fetch information about existing infrastructure.
Final Answer:
To get information about resources created outside Terraform -> Option A
Quick Check:
Data sources read existing resources = A [OK]
Hint: Data sources read info, they don't create or delete [OK]
Common Mistakes:
Thinking data sources create resources
Confusing data sources with resource blocks
Assuming data sources manage resource lifecycle
2. Which of the following is the correct syntax to define a data source in Terraform?
easy
A. output "aws_vpc" "main" { id = "vpc-123456" }
B. resource "aws_vpc" "main" { id = "vpc-123456" }
C. data "aws_vpc" "main" { id = "vpc-123456" }
D. variable "aws_vpc" "main" { id = "vpc-123456" }
Solution
Step 1: Identify Terraform block types
Terraform uses the keyword data to define data sources, resource for resources, output for outputs, and variable for input variables.
Step 2: Match syntax for data source
The correct syntax for a data source starts with data, followed by the provider and resource type, then the name and configuration.
Final Answer:
data "aws_vpc" "main" { id = "vpc-123456" } -> Option C
Quick Check:
Data source syntax starts with 'data' = A [OK]
Hint: Data sources start with 'data' keyword, not 'resource' [OK]
Common Mistakes:
Using 'resource' instead of 'data' for data sources
Confusing outputs or variables with data sources
Incorrect block structure or missing quotes
3. Given this Terraform data source snippet:
data "aws_subnet" "example" {
filter {
name = "tag:Name"
values = ["public-subnet"]
}
}
What does data.aws_subnet.example.id return?
medium
A. Creates a new subnet with the name public-subnet
B. Throws an error because filter is invalid
C. Returns a list of all subnet IDs in the AWS account
D. The ID of the subnet tagged with Name=public-subnet
Solution
Step 1: Understand the data source filter
The filter selects the subnet resource with the tag Name equal to "public-subnet".
Step 2: Determine the returned value
Accessing data.aws_subnet.example.id returns the ID of the subnet matching the filter, not creating or listing all subnets.
Final Answer:
The ID of the subnet tagged with Name=public-subnet -> Option D
Quick Check:
Filter selects existing subnet ID = B [OK]
Hint: Filters select existing resources; data source returns their attributes [OK]
Common Mistakes:
Thinking data source creates resources
Assuming it returns all subnets without filter
Misunderstanding filter syntax
4. You wrote this Terraform data source:
data "aws_security_group" "web" {
name = "web-sg"
}
But Terraform shows an error: "name" is not a valid argument. What is the likely fix?
medium
A. Remove the block entirely; data sources can't query security groups
B. Replace name with filter block to find security group by name
C. Add id argument instead of name
D. Change data to resource
Solution
Step 1: Check data source argument rules
The aws_security_group data source does not accept a direct name argument; it uses filters to find resources.
Step 2: Correct usage with filter block
Use a filter block with name = "group-name" and values = ["web-sg"] to query by name.
Final Answer:
Replace name with filter block to find security group by name -> Option B
Quick Check:
Use filter block for querying by name = D [OK]
Hint: Use filter blocks, not direct name arguments in data sources [OK]
Common Mistakes:
Using unsupported arguments in data sources
Confusing resource and data source syntax
Assuming data sources create or modify resources
5. You want to use a data source to get the latest AMI ID for Ubuntu in your region. Which approach correctly uses a data source to achieve this?
hard
A. data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}