What is the difference between a resource and a data source in Terraform?

Beginner

Answer

Resources are infrastructure components that Terraform creates, manages, and destroys. They represent the desired state of infrastructure.

Data sources allow Terraform to fetch information about existing infrastructure or external systems without managing them.

# 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
}