0
0
Terraformcloud~5 mins

Data source block syntax in Terraform - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a data block in Terraform?
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?<br>
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
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
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
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
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
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.