What are resources and data sources in Terraform?
Learn the difference between Terraform resources and data sources, with syntax, examples, interview tips and MCQs to master infrastructure as code.
Expected Interview Answer
In Terraform, a resource is a block that creates and manages a piece of infrastructure (like a VM, bucket, or DNS record), while a data source is a read-only block that fetches information about existing infrastructure Terraform does not manage.
Resources are declared with a `resource` block and appear in state; Terraform creates, updates, or destroys them to match your configuration. Data sources use a `data` block, perform no changes, and simply query a provider API to return attributes you can reference elsewhere. A common pattern is using a data source to look up an existing VPC or AMI ID and feeding that value into a resource you are creating.
- Resources give declarative, tracked lifecycle management
- Data sources let you reference infrastructure you do not own
- Avoids hardcoding IDs that change between environments
- Keeps configurations DRY across teams
- Enables safe integration with pre-existing systems
AI Mentor Explanation
A resource is like a player you personally pick and manage for your team — you decide their role, drop them, or swap them, and you are accountable for their spot. A data source is like reading the official ICC ranking of an opposing player: you consult that published number to plan your field, but you cannot change their ranking. Terraform builds and controls resources, and only reads data sources.
Step-by-Step Explanation
Step 1
Declare a resource
Use a `resource` block with a type and name; Terraform will create and track it in state.
Step 2
Reference existing infrastructure
Use a `data` block to query something Terraform does not manage, like an existing VPC or AMI.
Step 3
Wire outputs into inputs
Pass a data source attribute (for example, its ID) into a resource argument to avoid hardcoding.
Step 4
Plan and apply
Run `terraform plan` to preview creates/updates; data sources are read during refresh, resources during apply.
Step 5
Manage lifecycle
Update or destroy resources over time; data sources simply re-read on each run.
What Interviewer Expects
- Clear distinction between managed (resource) and read-only (data) blocks
- Understanding that resources appear in state and data sources are refreshed
- A realistic lookup example (AMI, VPC, availability zones)
- Awareness that data sources do not create or destroy anything
- Knowing how to reference attributes across blocks
Common Mistakes
- Thinking data sources create infrastructure
- Hardcoding IDs instead of looking them up with a data source
- Confusing the `data` block syntax with the `resource` block
- Forgetting that data sources still require a configured provider
- Assuming data source changes will trigger resource replacement automatically
Best Answer (HR Friendly)
“In Terraform, a resource is something you tell it to build and manage, like a server or database. A data source is read-only — it just looks up details about things that already exist so you can reuse them without typing out fixed values.”
Code Example
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-*"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "web-server"
}
}Follow-up Questions
- What happens to a data source during terraform apply?
- How do you avoid hardcoding an AMI or VPC ID?
- Can a data source depend on a resource created in the same run?
- What is the difference between a data source and a variable?
- How does Terraform state treat resources versus data sources?
MCQ Practice
1. Which block type creates and manages infrastructure in Terraform?
A `resource` block declares infrastructure Terraform creates, updates, and destroys, and it is tracked in state.
2. What is the primary purpose of a data source?
A data source is read-only; it queries an API for information about resources Terraform does not manage.
3. How do you reference a data source attribute?
Data source attributes are referenced using the data.<type>.<name>.<attribute> syntax.
Flash Cards
What is a Terraform resource? — A block that creates and manages a piece of infrastructure, tracked in state.
What is a Terraform data source? — A read-only block that fetches attributes of existing infrastructure Terraform does not manage.
How do you reference a data source ID? — Use data.<type>.<name>.id, for example data.aws_ami.ubuntu.id.
Do data sources appear in state? — They are recorded but Terraform never creates or destroys them — they are only refreshed/read.