Bird
Raised Fist0
Terraformcloud~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between a resource and a data source in Terraform?
easy
A. Resources and data sources both create infrastructure but in different clouds.
B. Resources create or manage infrastructure; data sources only read existing information.
C. Resources only read existing infrastructure; data sources create new infrastructure.
D. Resources are temporary; data sources are permanent.

Solution

  1. Step 1: Understand resource role

    Resources in Terraform are used to create or manage cloud infrastructure components.
  2. Step 2: Understand data source role

    Data sources are used to read information about existing infrastructure without changing it.
  3. Final Answer:

    Resources create or manage infrastructure; data sources only read existing information. -> Option B
  4. Quick Check:

    Resource = create/manage, Data source = read [OK]
Hint: Resources build; data sources just look up info [OK]
Common Mistakes:
  • Thinking data sources create resources
  • Confusing resource and data source roles
  • Assuming both modify infrastructure
  • Believing data sources are temporary
2. Which of the following is the correct syntax to declare a data source in Terraform?
easy
A. data "aws_ami" "example" {}
B. resource "aws_instance" "example" {}
C. source "aws_vpc" "example" {}
D. resource "data_aws_subnet" "example" {}

Solution

  1. Step 1: Identify data source syntax

    Data sources use the keyword data followed by the type and name.
  2. Step 2: Check options

    data "aws_ami" "example" {} uses data "aws_ami" "example" {}, which is correct syntax for a data source.
  3. Final Answer:

    data "aws_ami" "example" {} -> Option A
  4. Quick Check:

    Data source starts with 'data' keyword [OK]
Hint: Data sources start with 'data' keyword [OK]
Common Mistakes:
  • Using 'resource' keyword for data sources
  • Mixing 'source' keyword which doesn't exist
  • Prefixing resource with 'data_' incorrectly
  • Confusing resource and data source syntax
3. Given this Terraform snippet:
data "aws_vpc" "selected" {
  filter {
    name   = "tag:Name"
    values = ["main-vpc"]
  }
}

resource "aws_subnet" "example" {
  vpc_id     = data.aws_vpc.selected.id
  cidr_block = "10.0.1.0/24"
}

What does the data.aws_vpc.selected.id represent?
medium
A. The ID of an existing VPC filtered by tag "Name=main-vpc".
B. The ID of a subnet created inside the VPC.
C. A random VPC ID generated by Terraform.
D. The ID of a new VPC created by Terraform.

Solution

  1. Step 1: Analyze data source usage

    The data "aws_vpc" "selected" block reads an existing VPC filtered by the tag "Name=main-vpc".
  2. Step 2: Understand reference in resource

    The resource aws_subnet.example uses data.aws_vpc.selected.id to get the ID of that existing VPC to create a subnet inside it.
  3. Final Answer:

    The ID of an existing VPC filtered by tag "Name=main-vpc". -> Option A
  4. Quick Check:

    Data source reads existing VPC ID [OK]
Hint: Data source reads existing resource info [OK]
Common Mistakes:
  • Thinking data source creates new VPC
  • Confusing subnet ID with VPC ID
  • Assuming random ID generation
  • Using resource instead of data source for existing info
4. You wrote this Terraform code but get an error:
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

data "aws_vpc" "main" {
  id = aws_vpc.main.id
}

What is the problem?
medium
A. The data source block is missing a filter instead of id.
B. Data sources cannot reference resources in the same configuration.
C. You cannot use the same name for resource and data source.
D. The resource block is missing a provider configuration.

Solution

  1. Step 1: Check naming conflict

    The resource and data source both use the name "main" which causes a naming conflict in Terraform.
  2. Step 2: Terraform rules

    Terraform does not allow a resource and a data source to share the same type and name within the same module.
  3. Final Answer:

    You cannot use the same name for resource and data source. -> Option C
  4. Quick Check:

    Resource and data source names must be unique [OK]
Hint: Resource and data source names must differ [OK]
Common Mistakes:
  • Thinking same names cause conflicts
  • Believing you always need filters over id
  • Assuming data sources can depend on resources
  • Blaming missing provider config
5. You want to create a subnet in an existing VPC without recreating the VPC. Which Terraform approach is best?
hard
A. Use a resource block for the VPC and a resource block for the subnet.
B. Use a resource block for the subnet and ignore the VPC.
C. Use a data source block for both VPC and subnet.
D. Use a data source block to read the existing VPC and a resource block to create the subnet.

Solution

  1. Step 1: Identify existing infrastructure

    The VPC already exists, so it should not be recreated or managed as a resource.
  2. Step 2: Use data source for existing VPC

    Use a data source block to safely read the existing VPC's details without changing it.
  3. Step 3: Create subnet resource

    Create a resource block for the subnet to add new infrastructure inside the existing VPC.
  4. Final Answer:

    Use a data source block to read the existing VPC and a resource block to create the subnet. -> Option D
  5. Quick Check:

    Data source reads existing; resource creates new [OK]
Hint: Read existing with data source; create new with resource [OK]
Common Mistakes:
  • Trying to recreate existing VPC as resource
  • Using data source to create subnet (not possible)
  • Ignoring existing VPC and creating subnet without VPC ID
  • Using resource for both causing conflicts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard