Q: What is a `data` source in Terraform?
A data source in Terraform is a read-only lookup, declared with a data block, that fetches information about something Terraform does not manage in the current configuration, such as an existing VPC, the latest AMI or the current.
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: A <strong>data source in Terraform</strong> is a read-only lookup, declared with a <code>data</code> block, that fetches information about something Terraform does not manage in the current configuration, such as an existing VPC, the latest AMI or the current AWS account ID. Resources create and own infrastructure; data sources only read it. Interviewers ask this to check that you can integrate with existing infrastructure and other teams stacks without hard-coding IDs, and that you understand when the read happens, what happens if nothing matches and how data sources differ from variables and remote state. The answer should include a real example and the failure modes.
Mental model: data blocks versus resource blocks
The distinction is ownership: Terraform manages the lifecycle of resources and only queries data sources.
- Resource:
resource "aws_instance" "web"creates, updates and destroys a real object, and tracks it in state. - Data source:
data "aws_vpc" "main"asks the provider API for existing information. Terraform never creates or deletes the thing it reads. - Reference syntax: read values with the
data.prefix, for exampledata.aws_vpc.main.id. - State: results are stored in state so plans can use them, but they are refreshed on each plan and apply, so they reflect current reality.
- Why interviewers care: data sources are how Terraform composes with infrastructure created by other teams, the console or a different state, without importing it.
Hands-on: using a data source in Terraform
Typical uses are looking up AMIs, networks, account context and outputs from another state.
data "aws_caller_identity" "current" {}
data "aws_ami" "al2023" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-2023*-x86_64"]
}
}
data "aws_vpc" "main" {
tags = { Name = "prod-vpc" }
}
resource "aws_instance" "web" {
ami = data.aws_ami.al2023.id
instance_type = "t3.micro"
tags = { Owner = data.aws_caller_identity.current.account_id }
}
# Inspect what Terraform read
# terraform state list | grep '^data\.'
# terraform console -> data.aws_vpc.main.id
- Filter precisely: an
aws_amidata source needsownersandfilterblocks, otherwise it may match nothing or the wrong image. - Single match required: many data sources error if more than one result is found; use arguments like
most_recent = truewhere offered. - Cross-stack:
terraform_remote_statereads outputs from another state, though published outputs or SSM parameters create looser coupling. - Account context:
aws_caller_identityandaws_regionavoid hard-coding account IDs in ARNs. - Use with for_each: a data source can itself use
for_each, for example to look up several subnets by ID.
When data sources are read and how to verify
Reads happen at plan time when arguments are known, otherwise they are deferred until apply.
data "aws_subnet" "private" {
id = var.subnet_id
lifecycle {
postcondition {
condition = self.map_public_ip_on_launch == false
error_message = "Subnet must be private."
}
}
}
- Known arguments: the lookup runs during plan, so you can review the resolved value in the plan output.
- Unknown arguments: if an argument depends on a resource not yet created, the read is deferred to apply and values show as
(known after apply). - Validate results: use
preconditionandpostconditionblocks inlifecycle(Terraform 1.2 and later) to assert a data source returned what you expect. - Quick check:
terraform consoleevaluates expressions such asdata.aws_ami.al2023.nameagainst real state. - Refresh behaviour:
terraform plan -refresh-onlyshows how data source results changed without proposing infrastructure edits.
Failure modes and best practice
Data sources are convenient, but they introduce hidden dependencies on infrastructure you do not control.
- Nothing found: the plan fails with "no matching ... found". Check filters, region, account and permissions, since read permissions are also needed.
- Drift of inputs: a
most_recentAMI lookup can change on any plan and trigger instance replacement. Pin AMI names, or use SSM parameters, when stability matters. - Ordering: a data source that must read something created in the same run needs
depends_on, which can defer the read to apply; prefer passing resource attributes directly. - Secrets in state: reading a secret value into a data source stores it in state, so protect and encrypt the backend.
- Alternatives: if a value is a fixed setting rather than discoverable infrastructure, a variable is clearer than a lookup.
Common data source patterns in real projects
A few recurring patterns cover most real use.
- Existing network: look up a shared VPC and subnets by tag so application stacks do not hard-code IDs that differ per environment.
- IAM policy documents:
aws_iam_policy_documentbuilds JSON policies in HCL; it reads nothing remote but is a data source used for rendering. - Secrets and parameters:
aws_ssm_parameterreads published values from another team; mark outputs sensitive and remember state storage. - Zones and availability:
aws_availability_zonesyields valid zones per region so modules stay portable.
- A data source in Terraform is a read-only data block that looks up existing infrastructure or context, as opposed to a resource that Terraform creates and owns.
- I verify it with terraform plan, terraform console and state list, and add pre or postconditions to assert the result.
- I automate by replacing hard-coded IDs with data lookups and by pinning AMIs or reading published SSM parameters for cross-stack values.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Using a data source for something the same configuration creates Reference the resource attribute directly; a lookup adds an extra API call and can defer reads to apply time.
- Loose filters that match multiple objects Many data sources fail on multiple matches, and others pick an unexpected one, leading to wrong AMIs or subnets.
- Relying on most_recent for production AMIs A new image publication changes the plan and can force instance replacement without any code change.
- Forgetting the read permissions of the CI role Data sources call real provider APIs, so a role with write-only scope fails during plan.