Day 13: Terraform Data Sources – Reading Existing Infrastructure
What is a Terraform Data Source?
A data source lets you query existing resources in your AWS environment. Instead of creating a new VPC or AMI, you just read what’s already there.
Why Use Data Sources?
No Hardcoding: IDs change; code shouldn’t break.
Reuse Infra: Don’t reinvent the wheel.
Stay Current: Always fetch the latest info.
Example: Launching an EC2 Using Data Sources
Step 1: Provider Setup
We start by telling Terraform to use AWS.
provider "aws" {
region = "ap-south-1"
}
We set the region so all resources are consistent.
Step 2: VPC Data Source
We fetch the default VPC.
data "aws_vpc" "selected" {
default = true
}
default = true means we pick the default VPC—no need to hardcode VPC IDs.
Step 3: Subnet Data Source
Now we grab a subnet inside that VPC.
data "aws_subnet" "selected" {
filter {
name = "vpc-id"
values = [data.aws_vpc.selected.id]
}
filter {
name = "availability-zone"
values = ["ap-south-1a"]
}
}
We filter subnets in the chosen VPC and in a specific AZ.
Step 4: AMI Data Source
We fetch the latest Amazon Linux 2 AMI.
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
most_recent = true ensures we always get the latest image. We filter for Amazon Linux 2 AMIs only.
Step 5: EC2 Instance
Now we create the EC2 using those data sources.
resource "aws_instance" "example" {
ami = data.aws_ami.amazon_linux.id
instance_type = "t2.micro"
subnet_id = data.aws_subnet.selected.id
tags = {
Name = "terraform-ec2-demo"
}
}
We use the AMI ID from our AMI data source and the subnet ID from our subnet data source.
Step 6: Outputs (Optional)
We’ll output the instance details.
output "instance_id" {
value = aws_instance.example.id
}
output "instance_public_ip" {
value = aws_instance.example.public_ip
}
We output the instance ID and public IP for easy access.
Conclusion
Data sources make your Terraform code dynamic and reusable. You tap into what’s already there, making your infrastructure predictable and up-to-date.




