A resource is an object Terraform creates, updates and destroys, and its block describes the desired state of that object. A data source only reads information about something that already exists, and never changes it.
Terraform records each resource in state and owns its whole lifecycle. A data source fetches attributes of an object managed elsewhere, or of an external system. You reference it with the data. prefix, as in data.aws_vpc.default.id.
# Resource - creates new EC2 instance
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
# Data source - fetches existing VPC info
data "aws_vpc" "default" {
default = true
}
Use a resource for infrastructure this configuration should own. Use a data source when it only needs to look something up, such as the default VPC.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓