Data Sources
Imagine you’re building a Lego city with Terraform. You want to add a hospital, but instead of inventing a new design, you decide to look up the official Lego catalog to fetch the exact hospital set. In Terraform, this catalog lookup is done through data sources.
Data sources allow Terraform to query existing information from providers like available AMIs, VPC IDs, or subnet details so you don’t have to hardcode values. They make your configurations smarter, more dynamic, and adaptable to real‑world infrastructure.
What are Data Sources?
- Data sources let Terraform read information from providers.
- They don’t create resources; they fetch existing ones.
- Example: Querying the latest Amazon Machine Image (AMI) instead of hardcoding an ID.
Declaring Data Sources
Data sources are declared with the data block:
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}
Using Data Sources
You reference data sources with the data. prefix:
resource "aws_instance" "example" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
}
Here, Terraform fetches the latest Ubuntu AMI dynamically, ensuring your instance always uses the most up‑to‑date image.
Practical Use Cases
- Dynamic AMIs: Fetch the latest OS images.
- Networking: Query existing VPCs, subnets, or security groups.
- Cloud Services: Retrieve database endpoints or bucket names.
- Cross‑Team Collaboration: Use shared resources without hardcoding IDs.
Hands‑On Lab / Demo
Lab: Launch EC2 with Latest Ubuntu AMI
- Run:
terraform init→ initialize provider.terraform plan→ see AMI fetched dynamically.terraform apply→ provision EC2 with latest Ubuntu image.
Create main.tf:
provider "aws" {
region = "us-east-1"
}
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}
resource "aws_instance" "example" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
}
Pro Tips & Best Practices
- Use data sources to avoid hardcoding resource IDs.
- Combine data sources with variables for maximum flexibility.
- Document data sources clearly - others should know what’s being fetched.
- Be cautious: data sources query live provider data, so results may change over time.
- Use filters wisely to narrow down results.
Summary & Cheatsheet
- Data Sources = Fetch existing info.
- Declare with
datablock. - Reference with
data.prefix. - Use cases: AMIs, VPCs, subnets, shared resources.
Quick mnemonic: Data Sources = Query, Fetch, Reuse
The Hackers Notebook
Data sources make Terraform dynamic and intelligent. Instead of hardcoding values, you can query providers for the latest information, ensuring your infrastructure stays up‑to‑date and consistent. By mastering data sources, you’ve learned how to connect Terraform with real‑world cloud data.
