Skip to main content

Command Palette

Search for a command to run...

Day 13: Terraform Data Sources – Reading Existing Infrastructure

Updated
2 min read
P
Welcome! I’m Prajwal P. I stand at the intersection of technology and efficiency, exploring the dynamic world of DevOps ⚙️. From mastering Cloud infrastructure to orchestrating containers, I am passionate about automating the complex to create the simple. Join me as I document my learning curve, share technical insights, and navigate the ever-evolving landscape of software deployment.

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?

  1. No Hardcoding: IDs change; code shouldn’t break.

  2. Reuse Infra: Don’t reinvent the wheel.

  3. 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.


Terraform on AWS

Part 4 of 14

30 Days of Terraform on AWS is a hands-on series that takes you from IaC fundamentals to production-ready AWS architectures. Learn Terraform basics, AWS provisioning, mini projects, and advanced topics like EKS, CI/CD, GitOps, and Terraform Cloud.

Up next

Day11-12: Terraform Built-in Functions Explained with Real-World Examples

Terraform is not just about creating cloud resources.Its real power comes from built-in functions that allow you to transform data, validate inputs, read files, handle secrets, manage timestamps, and apply business logic to infrastructure. In this po...

More from this blog

Terraform on AWS

29 posts

Stop clicking in the AWS console. Start coding your infrastructure.

Day 13: Terraform Data Sources – Reading Existing Infrastructure