Terraform Data Sources

Terraform Data Sources
In: Terraform, AWS, NetDevOps

"Have you ever tried to find a specific book in a library without the help of the librarian or a catalogue? Well, that's like trying to find a subnet ID in AWS without Terraform data sources – nearly impossible!" 😅


Terraform defines datasources as:

Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration. Use of data sources allows a Terraform configuration to make use of information defined outside of Terraform, or defined by another separate Terraform configuration.

Think of data sources as the "Google" of your infrastructure. Instead of hardcoding values or manually looking up information, data sources provide a way to query and retrieve data automatically. This is especially useful when deploying resources across different Terraform modules (folders) or when working with resources created manually.

In this blog post, we'll dive into Terraform data sources and demonstrate how to use them to launch an EC2 instance in an existing AWS subnet. Let's get started!


Assuming you have an existing subnet in your AWS environment, we'll need to locate its ID to launch an EC2 instance within it. Instead of manually searching for the subnet ID, we can use the aws_subnet data source to query and retrieve this information dynamically. Subnet already exists in AWS with the id of subnet-0f2b77293ad28a714

Step 1 - Create a Data Source

Here's a simple example of how to use the aws_subnet data source to get the subnet ID based on a specific tag. In this configuration, we define a data source named get-subnet-id that filters AWS subnets by their "Name" tag. We then output the subnet ID to verify that the correct information has been fetched.

data "aws_subnet" "get-subnet-id" {
  filter {
    name   = "tag:Name"
    values = ["public-1"]


output "aws_subnet_id" {
  value = data.aws_subnet.get-subnet-id.id

Step -2 Create an EC2 Instance

Now that we've retrieved the subnet ID using the data source, let's use this information to launch an EC2 instance within the subnet. In this configuration, we create an EC2 instance using the aws_instance resource. Instead of hardcoding the subnet_id, we reference the data.aws_subnet.get-subnet-id.id data source to automatically fetch the desired subnet ID.

provider "aws" {
  region  = "eu-west-2"

resource "aws_instance" "ec2-ubuntu" {
  ami                    = "ami-0917237b4e71c5759"
  instance_type          = "t2.micro"
  subnet_id              = data.aws_subnet.get-subnet-id.id
  key_name               = "test-key"

  tags = {
    Name = "Ubuntu-1"

Step -3 Run Terraform

Suresh-MacBook:ec2 sureshvinasiththamby$ terraform apply
data.aws_subnet.get-subnet-id: Refreshing state...

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.ec2-ubuntu will be created
  + resource "aws_instance" "ec2-ubuntu" {
      + ami                          = "ami-0917237b4e71c5759"
      + instance_type                = "t2.micro"
      + key_name                     = "test-key"
      + source_dest_check            = true
      + subnet_id                    = "subnet-0f2b77293ad28a714"
      + tags                         = {
          + "Name" = "Ubuntu-1"
aws_instance.ec2-ubuntu: Creating...
aws_instance.ec2-ubuntu: Still creating... [10s elapsed]
aws_instance.ec2-ubuntu: Still creating... [20s elapsed]
aws_instance.ec2-ubuntu: Creation complete after 22s [id=i-09f8a11aee4eb13c3]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.


aws_subnet_id = subnet-0f2b77293ad28a714

As you can see above, Terraform picked up the correct subnet-id and launched an instance.

Benefits of Using Data Sources

  1. Dynamic configurations - Data sources allow your Terraform configurations to be more dynamic, automatically adapting to changes in your infrastructure.
  2. Modularity - When deploying resources across different modules, data sources enable you to access information without duplicating code or hardcoding values.
  3. Collaboration - Data sources make it easier to work with resources created by other teams or tools, helping to maintain a consistent infrastructure state.


In this blog post, we've explored Terraform data sources, specifically focusing on the aws_subnet data source. We demonstrated how to use it to dynamically retrieve the subnet ID and launch an EC2 instance within an existing AWS subnet. Data sources are an invaluable tool for managing complex infrastructures and promoting modularity and collaboration.

Written by
Suresh Vina
Tech enthusiast sharing Networking, Cloud & Automation insights. Join me in a welcoming space to learn & grow with simplicity and practicality.
More from Packetswitch
Table of Contents
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Packetswitch.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.