0
0
TerraformComparisonBeginner · 3 min read

Data Source vs Resource in Terraform: Key Differences and Usage

In Terraform, a resource creates or manages infrastructure components, while a data source reads or fetches information from existing infrastructure without changing it. Resources change your cloud environment; data sources only retrieve data for use in your configuration.
⚖️

Quick Comparison

This table summarizes the main differences between resource and data source in Terraform.

AspectResourceData Source
PurposeCreate or manage infrastructureRead existing infrastructure data
Effect on InfrastructureModifies or creates resourcesNo changes made
LifecycleHas create, update, delete actionsRead-only, no lifecycle actions
Use CaseProvision new componentsReference existing components
ExampleCreate an AWS EC2 instanceGet info about an existing AWS VPC
State StorageStored in Terraform stateData fetched during plan/apply and stored in state
⚖️

Key Differences

Resources in Terraform are the building blocks that define what infrastructure you want to create or manage. When you apply your Terraform configuration, resources are created, updated, or deleted to match your desired state. They have a full lifecycle and their changes are tracked in the Terraform state file.

On the other hand, data sources are used to fetch information about existing infrastructure that Terraform does not manage or that was created outside Terraform. They do not create or modify anything. Instead, they provide data that can be used as input for resources or outputs.

In simple terms, think of resources as the things you build or change, and data sources as the things you look up to get information. For example, you create a virtual machine with a resource, but you get the ID of an existing network with a data source.

⚖️

Code Comparison

Here is an example of a resource that creates an AWS S3 bucket.

terraform
resource "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-12345"
  acl    = "private"
}
Output
Creates a new private S3 bucket named 'my-unique-bucket-12345'.
↔️

Data Source Equivalent

Here is an example of a data source that reads information about an existing AWS S3 bucket.

terraform
data "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-12345"
}

output "bucket_arn" {
  value = data.aws_s3_bucket.example.arn
}
Output
Fetches the ARN of the existing S3 bucket 'my-unique-bucket-12345' without creating or modifying it.
🎯

When to Use Which

Choose resource when you want Terraform to create, update, or delete infrastructure components as part of your deployment. Use resource to build new things or manage existing ones fully.

Choose data source when you need to reference or use information about infrastructure that already exists outside Terraform or is managed elsewhere. Use it to read data without making changes.

In short, use resource to build and data source to look up.

Key Takeaways

Resources create or manage infrastructure and have a lifecycle in Terraform.
Data sources only read existing infrastructure data without making changes.
Use resources to build new components; use data sources to reference existing ones.
Resources affect Terraform state; data sources fetch data during plan or apply and store it in state.
Choosing correctly helps keep your infrastructure code clear and effective.