Key differences at a glance

Use resources when Terraform should own the lifecycle of an object. Use data sources when you only need to look up existing information (for example, a VNet ID or an AMI ID).
Examples (syntactic comparison)
resource example — Terraform will create and manage this virtual network:When to use each
-
Use
resourcewhen:- Terraform should create, update, or delete the object.
- You want the object tracked in Terraform state.
- You need full lifecycle management and drift detection.
-
Use
datawhen:- The object is managed outside this Terraform configuration (or by another team).
- You need to reference attributes (IDs, names, subnet lists, AMI IDs).
- You want to avoid Terraform creating duplicate or conflicting infrastructure.
Practical tips
- Prefer data sources for shared resources (e.g., centrally managed networks, shared subnets).
- Avoid using data sources as a workaround to hide unmanaged drift — if you need to manage something consistently, convert it into a resource.
- Remember that data lookups may introduce dependency ordering; reference them explicitly to ensure correct evaluation.