Resources vs Data Sources¶
Understanding the fundamental difference between resources and data sources in Terraform providers.
🤖 AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
Conceptual Difference¶
Resources: Infrastructure Management¶
Resources represent managed infrastructure - things your Terraform configuration creates, updates, and destroys.
Think of resources as: - Active management - Write operations - State tracking - Lifecycle management
Examples: - Creating a server - Managing a database - Configuring a firewall rule - Creating a DNS record
User intent: "Make this exist and keep it that way"
Data Sources: Information Retrieval¶
Data sources are read-only queries - they fetch information without creating or modifying anything.
Think of data sources as: - Passive querying - Read operations - No state (refreshed each run) - Information lookup
Examples: - Query available server images - Look up existing VPC - Fetch current IP address - Read file contents
User intent: "Tell me what exists"
Technical Differences¶
| Aspect | Resource | Data Source |
|---|---|---|
| Purpose | Manage infrastructure | Query information |
| Operations | Create, Read, Update, Delete | Read only |
| State | Tracked by Terraform | No state (re-queried) |
| Lifecycle | Full CRUD lifecycle | Single read operation |
| Changes | Can modify remote systems | Never modifies anything |
| Methods | read(), _create_apply(), _update_apply(), _delete_apply() |
read() only |
| Schema | Inputs + computed outputs | Inputs + computed outputs |
| Context | ResourceContext with state | Just config parameter |
When to Use Each¶
Use a Resource When:¶
✅ You need to create something
✅ You need to manage lifecycle (create, update, delete)
| Terraform | |
|---|---|
✅ Terraform should track state and detect drift
| Terraform | |
|---|---|
✅ You need update-in-place behavior
| Terraform | |
|---|---|
Use a Data Source When:¶
✅ You need to query existing infrastructure
| Terraform | |
|---|---|
✅ You need information from external systems
| Terraform | |
|---|---|
✅ You need dynamic lookups
| Terraform | |
|---|---|
✅ You need data that changes independently of Terraform
| Terraform | |
|---|---|
Design Philosophy¶
Resource Philosophy¶
"Declarative Infrastructure"
- User declares desired state
- Terraform makes it so
- Continuous reconciliation
- Provider owns the lifecycle
Resource contract:
| Text Only | |
|---|---|
Data Source Philosophy¶
"Information Bridge"
- User requests information
- Provider fetches it
- No ownership or management
- Fresh data on every run
Data source contract:
| Text Only | |
|---|---|
Common Anti-Patterns¶
❌ Using Resource for Read-Only Queries¶
| Terraform | |
|---|---|
Why bad: Resources should manage lifecycle. If this is read-only, it's a data source.
Fix:
| Terraform | |
|---|---|
❌ Using Data Source for Mutable State¶
| Terraform | |
|---|---|
Why bad: Data sources are read-only. This should be a resource property.
Fix:
| Terraform | |
|---|---|
❌ Storing Non-Deterministic Data¶
| Terraform | |
|---|---|
Why bad: Data sources should be deterministic (same inputs = same outputs).
Fix: If you need randomness, use a resource to track it:
| Terraform | |
|---|---|
Implementation Comparison¶
Resource Implementation¶
Complexity: Multiple methods, state management, lifecycle coordination
Data Source Implementation¶
| Python | |
|---|---|
Simplicity: One method, no state, just query and return
State Management¶
Resources Have State¶
Terraform tracks:
Changes are detected by comparing state with reality.
Data Sources Have No State¶
Terraform does not track state. Every terraform plan re-fetches the data.
Refresh Behavior¶
Resources: Drift Detection¶
terraform plancallsread()- Compares current state with desired state
- Shows what needs to change
Example:
| Text Only | |
|---|---|
Data Sources: Always Refresh¶
terraform plancallsread()- Uses fresh data immediately
- No comparison with previous run
Example:
| Text Only | |
|---|---|
When Something Could Be Either¶
Sometimes the same concept could be implemented as either:
Example: DNS Record¶
As a Resource:
| Terraform | |
|---|---|
As a Data Source:
| Terraform | |
|---|---|
Decision criteria: - Resource if you want Terraform to create/manage it - Data source if it exists externally and you just need its value
Summary¶
Resources = Active Management - Create, update, delete infrastructure - Terraform owns the lifecycle - State tracked and drift detected - Use for things Terraform should manage
Data Sources = Passive Queries - Read information only - No lifecycle management - No state tracking - Use for lookups and external data
See Also¶
- Building Your First Resource - Resource tutorial
- Building Your First Data Source - Data source tutorial
- Resource Lifecycle Reference - Resource API
- Data Source API Reference - Data source API