Table of Contents
What Are Data Sources?
Data sources let you fetch information about existing infrastructure that Terraform doesn’t manage. They’re read-only — they query but never create or modify resources.
Basic Usage
# Look up the latest Ubuntu AMI
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
}
Common Data Sources
AWS Account Info
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
locals {
account_id = data.aws_caller_identity.current.account_id
region = data.aws_region.current.name
}
Existing VPC
data "aws_vpc" "main" {
filter {
name = "tag:Name"
values = ["production-vpc"]
}
}
Availability Zones
data "aws_availability_zones" "available" {
state = "available"
}
SSM Parameter
data "aws_ssm_parameter" "db_host" {
name = "/prod/database/host"
}
Remote State
data "terraform_remote_state" "networking" {
backend = "s3"
config = {
bucket = "terraform-state"
key = "networking/terraform.tfstate"
region = "us-east-1"
}
}
# Use: data.terraform_remote_state.networking.outputs.vpc_id
Data Source vs Resource
| Aspect | Data Source | Resource |
|---|---|---|
| Purpose | Read existing | Create/manage |
| Prefix | data. | Direct reference |
| State | Refreshed every plan | Tracked in state |
| Lifecycle | Read-only | Full CRUD |
When to Use Data Sources
- Looking up AMIs — always use data sources for latest AMI
- Cross-stack references —
terraform_remote_statefor shared outputs - Existing infrastructure — query resources not managed by your config
- Dynamic values — availability zones, account ID, region
Best Practices
- Use
most_recent = truefor AMI lookups to avoid stale IDs - Use filters instead of hardcoding IDs — more portable
- Prefer outputs over remote_state — use Terraform Cloud or parameter store for cross-stack data
- Data sources refresh every plan — be aware of API rate limits
Learn More
- Terraform for Beginners Course — hands-on data source labs
- Terraform By Example Book — real-world patterns
- Terraform Cheat Sheet — quick command reference



