0
0
Terraformcloud~10 mins

Data source vs resource difference in Terraform - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - Data source vs resource difference
Start Terraform Apply
Check for Data Sources
Read Existing Infrastructure
Check for Resources
Create or Update Infrastructure
Finish Apply
Terraform first reads data sources to get info about existing infrastructure, then creates or updates resources to build new infrastructure.
Execution Sample
Terraform
data "aws_ami" "example" {
  most_recent = true
  owners      = ["amazon"]
}

resource "aws_instance" "example" {
  ami           = data.aws_ami.example.id
  instance_type = "t2.micro"
}
This code reads the latest Amazon Linux AMI (data source) and then creates an EC2 instance (resource) using that AMI.
Process Table
StepActionData Source ReadResource CreatedResult
1Start terraform applyNoNoBegin execution
2Read data source aws_ami.exampleReads latest Amazon Linux AMI IDNoAMI ID fetched from AWS
3Create resource aws_instance.exampleNoCreates EC2 instance with fetched AMI IDEC2 instance launched
4Finish applyNoNoInfrastructure updated successfully
💡 All data sources read before resources created; apply completes after resource creation
Status Tracker
VariableStartAfter Step 2After Step 3Final
data.aws_ami.example.idundefinedami-0abcdef1234567890ami-0abcdef1234567890ami-0abcdef1234567890
resource.aws_instance.example.idundefinedundefinedi-0123456789abcdef0i-0123456789abcdef0
Key Moments - 3 Insights
Why does Terraform read data sources before creating resources?
Terraform reads data sources first to get information about existing infrastructure it needs to reference when creating or updating resources, as shown in step 2 of the execution_table.
Can data sources create or change infrastructure?
No, data sources only read existing infrastructure data; they do not create or modify anything. This is clear because resource creation happens only after data source reading (step 3).
What happens if the data source changes between applies?
Terraform will fetch the latest data each apply, so resources depending on data sources may be updated accordingly, reflecting the new data source values as tracked in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of data.aws_ami.example.id after step 2?
Aami-0abcdef1234567890
Bundefined
Ci-0123456789abcdef0
Dt2.micro
💡 Hint
Check the 'Data Source Read' column in row for step 2 in execution_table
At which step does Terraform create the EC2 instance resource?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Resource Created' column in execution_table
If the data source returned a different AMI ID, how would variable_tracker change after step 2?
ANo variables would change
Bresource.aws_instance.example.id would change immediately
Cdata.aws_ami.example.id would have the new AMI ID value
DTerraform would skip resource creation
💡 Hint
Variable tracker shows data source values after step 2; resource IDs update after resource creation
Concept Snapshot
Terraform applies in two phases:
1. Data sources read existing infrastructure info.
2. Resources create or update infrastructure.
Data sources do NOT create or change anything.
Resources depend on data source info to build new infrastructure.
Full Transcript
Terraform uses data sources to read existing infrastructure details before creating or updating resources. In the example, Terraform first reads the latest Amazon Linux AMI ID using a data source, then creates an EC2 instance resource using that AMI. The execution flow starts with reading data sources, then proceeds to resource creation. Variables track the AMI ID after reading the data source and the EC2 instance ID after resource creation. Beginners often wonder why data sources run first, which is to provide necessary info for resource creation. Data sources do not create or modify infrastructure; only resources do. If data source info changes, Terraform fetches the new data on the next apply, which may affect resource updates.