0
0
Terraformcloud~10 mins

Data source block syntax in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Data source block syntax
Start Terraform config
Define data source block
Specify provider and data source type
Set required arguments
Terraform reads data source
Data available for use in resources
End
Terraform reads the data source block to fetch existing info, making it available for resource configuration.
Execution Sample
Terraform
data "aws_ami" "example" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }
}
This data block fetches the most recent Amazon Linux 2 AMI owned by Amazon.
Process Table
StepActionEvaluationResult
1Terraform reads 'data' block type and namedata "aws_ami" "example"Identifies data source type 'aws_ami' with local name 'example'
2Reads attribute 'most_recent'most_recent = trueSets to fetch the latest AMI
3Reads attribute 'owners'owners = ["amazon"]Filters AMIs owned by Amazon
4Reads 'filter' blockfilter { name = "name", values = ["amzn2-ami-hvm-*-x86_64-gp2"] }Filters AMIs matching name pattern
5Terraform queries AWS API with filtersAPI call with filtersReturns AMI data matching criteria
6Data source 'example' populatedData availableAMI ID and attributes ready for use
7Terraform completes data source readEnd of data block processingData source ready for resource references
💡 Terraform finishes reading data source block and stores fetched data for later use.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
most_recentundefinedtruetruetruetruetrue
ownersundefinedundefined["amazon"]["amazon"]["amazon"]["amazon"]
filter_nameundefinedundefinedundefined"name""name""name"
filter_valuesundefinedundefinedundefined["amzn2-ami-hvm-*-x86_64-gp2"]["amzn2-ami-hvm-*-x86_64-gp2"]["amzn2-ami-hvm-*-x86_64-gp2"]
data_source_resultundefinedundefinedundefinedundefinedAMI data objectAMI data object
Key Moments - 3 Insights
Why do we use 'data' blocks instead of 'resource' blocks for existing infrastructure?
Data blocks fetch existing info without creating or changing it, as shown in execution_table step 5 where Terraform queries AWS to get data but does not create anything.
What happens if the filter values do not match any existing resource?
Terraform will fail to find data and throw an error during the data source read phase (execution_table step 5), because data sources must return existing info.
Can we use variables inside data source blocks?
Yes, variables can be used to set attributes inside data blocks before Terraform reads them, similar to how 'owners' or 'filter.values' could be dynamic.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Terraform actually fetch data from the cloud provider?
AStep 3
BStep 5
CStep 2
DStep 7
💡 Hint
Refer to execution_table row with 'Terraform queries AWS API with filters'
According to variable_tracker, what is the value of 'most_recent' after step 4?
Atrue
Bfalse
Cundefined
Dnull
💡 Hint
Check the 'most_recent' row under 'After Step 4' column in variable_tracker
If we remove the 'owners' attribute, what would likely happen during execution?
ATerraform will fail to parse the data block
BTerraform will ignore the data source
CTerraform will fetch all AMIs without owner filter
DTerraform will create a new AMI
💡 Hint
Consider how filters affect data source queries as shown in execution_table steps 3 and 5
Concept Snapshot
Terraform data source block syntax:
- Use 'data' keyword with type and name
- Set required arguments inside block
- Terraform reads and queries existing resources
- Data is read-only and used in resource configs
- Filters narrow down data source results
- Data source must return existing info or error
Full Transcript
This visual execution shows how Terraform processes a data source block. It starts by reading the block type and name, then reads attributes like 'most_recent' and 'owners'. Next, it reads any filter blocks to narrow the search. Terraform then queries the cloud provider API with these filters to fetch existing resource data. The fetched data is stored and made available for use in resource definitions. Variables like 'most_recent' and 'owners' are set before the query. If filters do not match any resource, Terraform errors. Data sources do not create or modify resources; they only read existing ones.