Querying Data with Data Sources
Utilize data sources to fetch information about existing infrastructure or external services, allowing for more dynamic and adaptive configurations.
Querying Data with Data Sources is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Data Sources?
In Terraform, we often need to reference existing infrastructure or external data that isn't managed by our current configuration.
This is where Data Sources come in! They allow your Terraform configuration to fetch information about resources that have already been provisioned or external services.
Data Sources vs. Resources
It's important to distinguish data sources from resources.
- Resources define infrastructure that Terraform will create, update, or delete.
- Data Sources only fetch information about existing infrastructure or data. They do not create or modify anything.
Think of them as read-only queries for your infrastructure.
Practical Use Cases
Data sources are incredibly useful for:
- Finding the ID of an existing Virtual Private Cloud (VPC).
- Getting the latest Amazon Machine Image (AMI) for an EC2 instance.
- Retrieving secrets from a secret management service.
- Looking up DNS records or network configurations.
They make your configurations more dynamic and less hardcoded.
Defining a Data Source
A data source block looks similar to a resource block. It starts with the data keyword, followed by the provider and type, and then a local name.
data "aws_vpc" "selected" {
filter {
name = "tag:Name"
values = ["my-existing-vpc"]
}
}Using Fetched Data
Once a data source fetches information, its attributes become available to other parts of your configuration. You can reference them using the syntax: data.<TYPE>.<NAME>.<ATTRIBUTE>.
For example, to get the ID of the VPC we selected:
output "vpc_id" {
value = data.aws_vpc.selected.id
}Fetching the Latest AMI
A common task is finding the latest Amazon Machine Image (AMI) for an EC2 instance. This avoids hardcoding AMI IDs, making your config more robust.
Here's how to fetch the latest Amazon Linux 2 AMI:
provider "aws" {
region = "us-east-1"
}
data "aws_ami" "amazon_linux_2" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
output "latest_ami_id" {
value = data.aws_ami.amazon_linux_2.id
}Dynamic S3 Policy Document
Data sources aren't just for cloud resources. The aws_iam_policy_document data source helps construct complex IAM policies in a structured way.
It doesn't create a policy, but generates the JSON document you can then apply to a resource like an S3 bucket.
provider "aws" {
region = "us-east-1"
}
data "aws_iam_policy_document" "s3_read_only_policy" {
statement {
principals {
type = "AWS"
identifiers = ["arn:aws:iam::123456789012:user/example-user"]
}
actions = [
"s3:GetObject",
"s3:ListBucket"
]
resources = [
"arn:aws:s3:::my-example-bucket",
"arn:aws:s3:::my-example-bucket/*"
]
}
}
output "policy_json" {
value = data.aws_iam_policy_document.s3_read_only_policy.json
}Refining Your Search
Many data sources support filter blocks. These allow you to specify criteria to narrow down the results and ensure you fetch the correct resource or data.
Filters typically accept a name (the attribute to filter by) and values (a list of acceptable values for that attribute).
data "aws_instance" "web" {
instance_state_names = ["running"]
filter {
name = "tag:Environment"
values = ["production"]
}
filter {
name = "instance-type"
values = ["t2.micro", "t3.micro"]
}
}Data Source Best Practices
To keep your configurations clean and efficient:
- Be Specific: Use filters to ensure you fetch the exact data you need, avoiding ambiguity.
- Idempotency: Data sources are inherently idempotent, meaning running them multiple times produces the same result.
- Minimize Calls: Fetch data only when necessary to speed up
terraform planoperations. - Combine with Outputs: Use outputs to expose fetched data that other configurations or users might need.
Data Source Quiz
Which of the following statements are TRUE about Terraform Data Sources?
Summary: Querying Data
You've learned how Terraform data sources enable your configurations to query and fetch information about existing infrastructure or external services.
This powerful feature allows for dynamic, flexible, and robust configurations by avoiding hardcoded values and adapting to your environment's current state. Remember, data sources only read, they never create or modify!
Frequently asked questions
Is the “Querying Data with Data Sources” lesson free?
Yes — the full text of “Querying Data with Data Sources” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Querying Data with Data Sources”?
Utilize data sources to fetch information about existing infrastructure or external services, allowing for more dynamic and adaptive configurations. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Querying Data with Data Sources” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Querying Data with Data Sources
- Local and Remote Exec Provisioners
- Null Resources and Triggers
- Using External Data and Templates