Terraform Infrastructure as CodeTerraformTerraform Data Sources

Article & Tutorial

Terraform Data Sources Explained: Read Existing Cloud Resources

Learn Terraform data sources, how they read existing cloud resources, when to use them, common examples, mistakes, and best practices.

Infrastructure code reading existing cloud resources through a data lookup layer
Infrastructure code reading existing cloud resources through a data lookup layer
Contents

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.

example
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.

Read related Terraform guide

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.

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.