0
0
Terraformcloud~5 mins

Why data sources query existing infrastructure in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
Sometimes you need to use information about resources that already exist outside your current setup. Data sources let you look up this information so you can use it in your Terraform configuration without recreating those resources.
When you want to find the ID of a network that was created manually or by another team.
When you need to get the latest AMI image ID from AWS to launch a server.
When you want to use an existing database instance in your Terraform setup without managing it.
When you need to reference existing security groups to attach to new resources.
When you want to read configuration details from cloud resources created outside Terraform.
Config File - main.tf
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"
}

The provider block sets the AWS region.

The data "aws_ami" "ubuntu" block queries AWS for the most recent Ubuntu AMI owned by the official Ubuntu account.

The aws_instance resource uses the AMI ID from the data source to launch a server.

Commands
This command initializes the Terraform working directory, downloads the AWS provider plugin, and prepares Terraform to run.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do. It queries the data source to get the latest AMI ID and plans to create an EC2 instance using that AMI.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... 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.example will be created + resource "aws_instance" "example" { + ami = "ami-0abcdef1234567890" + arn = (known after apply) + instance_type = "t2.micro" + id = (known after apply) + tags = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the planned changes, creating the EC2 instance with the AMI ID obtained from the data source.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 20s [id=i-0123456789abcdef0] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command displays the current state of the infrastructure Terraform manages, showing the EC2 instance details including the AMI ID used.
Terminal
terraform show
Expected OutputExpected
aws_instance.example: id = i-0123456789abcdef0 ami = ami-0abcdef1234567890 instance_type = t2.micro ...
Key Concept

Data sources let Terraform look up existing resources so you can use their details without managing or recreating them.

Common Mistakes
Trying to use a data source without initializing Terraform first.
Terraform needs to download providers and prepare the environment before it can query data sources.
Always run 'terraform init' before 'terraform plan' or 'terraform apply'.
Assuming data sources create or change resources.
Data sources only read existing information; they do not create or modify anything.
Use data sources only to fetch information, and use resources to create or change infrastructure.
Using incorrect filters in data sources causing no results or errors.
If filters do not match any existing resource, Terraform cannot find the data and will error.
Check filter values carefully and test queries to ensure they match existing resources.
Summary
Use 'terraform init' to prepare Terraform and download necessary plugins.
Use data sources in your configuration to query existing infrastructure details.
Run 'terraform plan' to see what Terraform will do using data from data sources.
Apply changes with 'terraform apply' to create resources using queried data.
Use 'terraform show' to verify the current state and resource details.