A data block lets you fetch information from existing resources outside your Terraform configuration. It helps you use data created elsewhere.
Click to reveal answer
beginner
What are the main parts of a Terraform data source block?
A data source block has three parts: the keyword data, the resource type (like aws_ami), and a name you choose to reference it later.
Click to reveal answer
beginner
How do you reference a data source output in Terraform?
Use data.<resource_type>.<name>.<attribute>. For example, data.aws_ami.example.id gets the AMI ID.
Click to reveal answer
intermediate
Example: What does this data block do?
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}
It finds the most recent Ubuntu 20.04 AMI owned by the official Ubuntu account. This lets you use the latest Ubuntu image in your infrastructure.
Click to reveal answer
beginner
Why use data sources instead of hardcoding values?
Data sources keep your config flexible and up-to-date. They fetch live info, so you avoid errors from outdated or manual values.
Click to reveal answer
What keyword starts a data source block in Terraform?
Adata
Bresource
Cvariable
Doutput
✗ Incorrect
The data keyword defines a data source block to fetch existing info.
How do you name a data source block?
AYou use the resource type only
BYou use the provider name
CYou provide a unique name after the resource type
DYou don't name data sources
✗ Incorrect
You give a unique name after the resource type to reference the data source later.
Which of these is a valid way to reference a data source attribute?
Aoutput.aws_instance.example.id
Bdata.aws_instance.example.id
Cvar.aws_instance.example.id
Dresource.aws_instance.example.id
✗ Incorrect
Data source attributes are referenced with data.<type>.<name>.<attribute>.
What does the filter block inside a data source do?
AIt sets variables
BIt creates a new resource
CIt deletes resources
DIt filters which existing resources to fetch
✗ Incorrect
Filters narrow down which existing resources the data source returns.
Why is using data sources considered a best practice?
AThey fetch live data to keep configs current
BThey hardcode values for stability
CThey speed up Terraform runs
DThey replace variables
✗ Incorrect
Data sources fetch live info, avoiding stale or manual values.
Explain the structure and purpose of a Terraform data source block.
Think about how you tell Terraform to look up info it doesn't create.
You got /5 concepts.
Describe how filters work inside a data source block and why they are useful.
Filters help you find exactly what you need from many options.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a data source block in Terraform?
easy
A. To create new cloud resources from scratch
B. To define variables for Terraform configuration
C. To delete existing cloud resources
D. To read information about existing cloud resources without creating new ones
Solution
Step 1: Understand the role of data sources
Data sources in Terraform are used to fetch information about existing resources, not to create or delete them.
Step 2: Compare options with this role
Only To read information about existing cloud resources without creating new ones describes reading existing resource info without creation, matching the data source purpose.
Final Answer:
To read information about existing cloud resources without creating new ones -> Option D
Quick Check:
Data source = read existing info [OK]
Hint: Data sources read existing resources, not create or delete [OK]
Common Mistakes:
Confusing data sources with resource blocks
Thinking data sources create resources
Assuming data sources delete resources
2. Which of the following is the correct syntax to define a data source block for an AWS AMI in Terraform?
easy
A. data "aws_ami" example { filter { name = "name" values = ["my-ami"] } }
B. resource "aws_ami" "example" { filter { name = "name" values = ["my-ami"] } }
C. data "aws_ami" "example" { filter { name = "name" values = ["my-ami"] } }
D. data aws_ami example { filter { name = "name" values = ["my-ami"] } }
Solution
Step 1: Identify correct block type and syntax
Data source blocks start with data, followed by resource type and local name in quotes.
Step 2: Check each option
data "aws_ami" "example" { filter { name = "name" values = ["my-ami"] } } uses correct quotes and block structure. resource "aws_ami" "example" { filter { name = "name" values = ["my-ami"] } } uses resource instead of data. Options A and C miss quotes around local name or resource type.
Final Answer:
data "aws_ami" "example" { filter { name = "name" values = ["my-ami"] } } -> Option C
Quick Check:
Data source syntax = data "type" "name" { ... } [OK]
Hint: Data source block uses data "type" "name" with quotes [OK]
Common Mistakes:
Using resource instead of data
Omitting quotes around type or name
Incorrect block nesting or missing braces
3. Given this Terraform data source block:
data "aws_vpc" "selected" {
filter {
name = "tag:Name"
values = ["main-vpc"]
}
}
What will data.aws_vpc.selected.id return?
medium
A. The ID of the VPC tagged with Name=main-vpc
B. The name tag value main-vpc
C. A list of all VPC IDs in the AWS account
D. An error because filter syntax is invalid
Solution
Step 1: Understand the filter block
The filter selects the VPC resource with tag Name equal to main-vpc.
Step 2: Identify the returned attribute
data.aws_vpc.selected.id accesses the ID of the matched VPC, not the tag value or a list.
Final Answer:
The ID of the VPC tagged with Name=main-vpc -> Option A
Quick Check:
Filter selects resource, .id returns its ID [OK]
Hint: Filter narrows resource, .id returns its unique ID [OK]
Common Mistakes:
Confusing tag value with resource ID
Expecting a list instead of single ID
Assuming filter syntax is wrong
4. Identify the error in this Terraform data source block:
data "aws_subnet" "example" {
filter {
name = "availability-zone"
values = "us-west-2a"
}
}
medium
A. The values attribute should be a list, not a string
B. The filter block is missing a closing brace
C. The data source type aws_subnet does not support filters
D. The local name example must be uppercase
Solution
Step 1: Check the values attribute type
In Terraform, filter values must be a list, even if only one value is given.
Step 2: Verify the given values
The code uses a string instead of a list, which causes a syntax error.
Final Answer:
The values attribute should be a list, not a string -> Option A
Quick Check:
Filter values = list, not string [OK]
Hint: Always use list syntax ["value"] for filter values [OK]
Common Mistakes:
Using string instead of list for values
Assuming filter block needs no braces
Thinking local name case matters
5. You want to use a data source block to get the latest Amazon Linux 2 AMI ID filtered by owner and name pattern. Which of these blocks correctly achieves this?