Course contentsShow
← Cloud and Infrastructure
Lesson 747 of 2,98914. Terraform FundamentalsFree lesson

Data Sources Overview

Understand data sources as read-only queries to fetch information about existing infrastructure outside Terraform's management.

Data Sources Overview

What you'll learn: How to use Terraform data sources to read information about infrastructure that already exists, without managing it yourself.


What Are Data Sources?

In Terraform, data sources let you query and fetch information about existing infrastructure—things you didn't create with your current Terraform configuration and don't want to manage. Think of them as read-only lookups: you're asking, "What's already out there?" rather than creating something new.

Unlike resource blocks (which define infrastructure you want Terraform to create and manage), data sources are purely informational. They don't create, modify, or delete anything. They simply retrieve data you can reference elsewhere in your configuration.

Why Use Data Sources?

Imagine you're deploying an application server, but your networking team has already set up a VPC and subnets in AWS. You need to know the subnet ID to place your server in the right location. Rather than hard-coding that ID (which might change), you use a data source to look it up dynamically.

Data sources help you:

  • Reference infrastructure managed by other teams or configurations
  • Fetch provider-managed information (like available machine types or regions)
  • Keep your code flexible and avoid hard-coded values
  • Integrate new Terraform code with existing infrastructure

How They Differ from Resources

Resources use resource blocks and represent infrastructure Terraform creates and controls. Data sources use data blocks and represent infrastructure Terraform only reads about. You query it, reference its attributes, but never change it through that data source.

Key Takeaway: Data sources are read-only queries that let Terraform discover and reference existing infrastructure without taking ownership of it, enabling seamless integration with resources outside your management scope.