Terraform Data Sources Explained
Learn how Terraform data sources read existing infrastructure, lookup cloud resources, and connect reusable Terraform code with real environments.
Quick answer
Resource
Creates or manages infrastructure.
Data source
Reads existing information from a provider.
What is a Terraform data source?
A data source lets Terraform read information about existing resources without managing their full lifecycle.
Example
data "aws_ami" "ubuntu" { most_recent = true owners = ["099720109477"] filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] } }
Using the data source
resource "aws_instance" "web" { ami = data.aws_ami.ubuntu.id instance_type = "t3.micro" }
Common use cases
- Find latest AMI/image.
- Read existing VPC/subnet.
- Lookup DNS zone.
- Read existing key vault or secret reference.
- Fetch Kubernetes cluster information.
Common mistakes
- Using data sources when Terraform should manage the resource.
- Using broad filters that match the wrong resource.
- Not understanding that data sources are read during plan/apply.
Best practices
- Use precise filters.
- Name data sources clearly.
- Document externally managed resources.
- Avoid relying on unstable lookup results for production.
Related guides
- Terraform Learning Path
- Terraform Providers Explained
Official references
- Terraform data sources documentation
Data sources can fail if lookup filters are ambiguous
This fragment reads the currently authenticated AWS account ID. It requires a configured AWS provider. For AMI or subnet queries, use exact filters and verify uniqueness: an unexpectedly selected existing resource can cause incorrect placement even though Terraform validation passes.
data "aws_caller_identity" "current" {}
output "aws_account_id" {
value = data.aws_caller_identity.current.account_id
}Related Melochip article: https://melochip.com/posts/terraform-aws-provider-beginner-guide. Examples are illustrative and have not been independently executed.
Practical guidance and verification
Understand data source ownership
A data source reads information about an existing object, such as an image or VPC. Unlike a managed resource, it does not normally create the object or assign Terraform ownership of its lifecycle. This makes data sources useful when infrastructure is maintained by another team or platform. Use precise identifiers rather than vague search filters.
Read timing and permissions
A data read might be deferred until apply when its inputs are unknown during planning. Verify account, provider region and permissions to prevent the same configuration resolving a different object across environments. Fail early when the expected object is absent instead of silently using an unrelated match.
Keep returned information private
Some data-source attributes are sensitive and can be captured in state or exposed through outputs. Apply least privilege to the provider and state backend, and do not print secrets during troubleshooting. Consult https://developer.hashicorp.com/terraform/language/data-sources.
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026Related articles
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes monitoring and logs with kubectl logs, events, metrics-server, top commands, Prometheus, Graf...
6 min read · Updated Nov 16, 2026
Learn Kubernetes Ingress with controllers, IngressClass, host and path routing, TLS, DNS, annotations, redirec...
8 min read · Updated Nov 16, 2026
Learn Kubernetes ConfigMaps and Secrets with YAML, environment variables, file mounts, update behavior, immuta...
8 min read · Updated Nov 16, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.