0
0
Terraformcloud~5 mins

Data source vs resource difference in Terraform - CLI Comparison

Choose your learning style9 modes available
Introduction
When managing infrastructure with Terraform, you often need to use existing resources or create new ones. Data sources let you read information about existing infrastructure, while resources let you create or change infrastructure. Understanding the difference helps you manage your cloud setup correctly.
When you want to use details of an existing cloud resource without changing it, like reading a VPC ID.
When you need to create a new server, database, or network component from scratch.
When you want to reference existing infrastructure in your Terraform code without managing its lifecycle.
When you want Terraform to manage the lifecycle of a resource, including creation, update, and deletion.
When you want to combine existing infrastructure data with new resources in your setup.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

# Data source to read existing VPC
 data "aws_vpc" "existing_vpc" {
  default = true
}

# Resource to create a new EC2 instance
 resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id     = data.aws_vpc.existing_vpc.default_network_acl_id
  tags = {
    Name = "example-instance"
  }
}

The provider block sets the AWS region.

The data block reads information about the default VPC without changing it.

The resource block creates a new EC2 instance using the AMI and instance type specified. It uses the subnet ID from the existing VPC data source.

Commands
This command initializes the Terraform working directory. It downloads the AWS provider plugin needed to manage AWS resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command shows what Terraform will do. It reads the existing VPC data source and plans to create a new EC2 instance using that information.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + subnet_id = "subnet-12345678" + tags = { + "Name" = "example-instance" } } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the planned changes. It creates the new EC2 instance using the subnet from the existing VPC data source.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 20s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command shows the current state of the infrastructure Terraform manages, including the new EC2 instance and the data source information.
Terminal
terraform show
Expected OutputExpected
aws_instance.example: id = i-0abcd1234efgh5678 ami = ami-0c55b159cbfafe1f0 instance_type = t2.micro subnet_id = subnet-12345678 Data Source aws_vpc.existing_vpc: id = vpc-87654321 default_subnet_id = subnet-12345678
Key Concept

Data sources let you read existing infrastructure without changing it, while resources let you create or manage infrastructure.

Common Mistakes
Using a data source when you want Terraform to create or manage a resource.
Data sources only read existing resources and do not create or change anything, so Terraform will not manage the lifecycle.
Use a resource block when you want Terraform to create, update, or delete infrastructure.
Trying to create a resource with the same name or ID as an existing resource read by a data source.
This can cause conflicts or errors because Terraform tries to create something that already exists.
Use data sources to reference existing resources and resources to create new unique infrastructure.
Summary
Use data sources to read information about existing infrastructure without changing it.
Use resources to create and manage new infrastructure components.
Run 'terraform init' to prepare, 'terraform plan' to see changes, and 'terraform apply' to make changes.